PHP mySQL - 从dirrefent表中选择未使用的唯一值
更新:请参见下文
我有一个表:data
+-----------------------+--------------+-----------+
| State | d_country | d_postcode|
+-----------------------+--------------+-----------+
| State1 | Country1 | 1111 |
| State2 | Country2 | 2222 |
| State3 | Country3 | 3333 |
| State4 | Country4 | 4444 |
+-----------------------+--------------+-----------+
另一个表:user
+-----------------------+--------------+-----------+
| Name | u_country | u_postcode|
+-----------------------+--------------+-----------+
| Name1 | Country3 | 3333 |
| Name2 | Country5 | 5555 |
| Name3 | | 6666 |
| Name4 | Country6 | 6666 |
| Name5 | Country6 | 6666 |
+-----------------------+--------------+-----------+
我应该使用什么 SQL:
确定表数据中未列出的国家/地区的数量(计数)。例如,u_postcode
未在 d_postcode
中列出,为 5555 和 6666。它将返回 2。
列出表数据中不可用的名称和国家/地区
更新
- 我想使用分组来过滤邮政编码并使Name3和Name4成为不同的行。
例如:
+-----------------------+--------------+-----------+
| Name | u_country | u_postcode|
+-----------------------+--------------+-----------+
| Name2 | Country5 | 5555 |
| Name3 | | 6666 |
| Name4 | Country6 | 6666 |
+-----------------------+--------------+-----------+
有什么可能的想法吗?
备注:更正我自己
SELECT DISTINCT Name, u_country, u_postcode FROM user
WHERE u_postcode NOT IN (SELECT d_postcode FROM data)
Updates : Please see below
i have table: data
+-----------------------+--------------+-----------+
| State | d_country | d_postcode|
+-----------------------+--------------+-----------+
| State1 | Country1 | 1111 |
| State2 | Country2 | 2222 |
| State3 | Country3 | 3333 |
| State4 | Country4 | 4444 |
+-----------------------+--------------+-----------+
And another table: user
+-----------------------+--------------+-----------+
| Name | u_country | u_postcode|
+-----------------------+--------------+-----------+
| Name1 | Country3 | 3333 |
| Name2 | Country5 | 5555 |
| Name3 | | 6666 |
| Name4 | Country6 | 6666 |
| Name5 | Country6 | 6666 |
+-----------------------+--------------+-----------+
What SQL should i use to:
Determine the number (count) of country that are not listed on table data. For example u_postcode
is not listed in d_postcode
is 5555 and 6666. It will return 2.
List down name and what country not available in table data yet.
Updates
- I want to use grouping to filter postcode and make Name3 and Name4 as different rows.
For example:
+-----------------------+--------------+-----------+
| Name | u_country | u_postcode|
+-----------------------+--------------+-----------+
| Name2 | Country5 | 5555 |
| Name3 | | 6666 |
| Name4 | Country6 | 6666 |
+-----------------------+--------------+-----------+
Any possible idea?
Remarks: Corrected myself
SELECT DISTINCT Name, u_country, u_postcode FROM user
WHERE u_postcode NOT IN (SELECT d_postcode FROM data)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个。
希望有帮助
Try this.
Hope it helps