PHP mySQL - 从dirrefent表中选择未使用的唯一值

发布于 2024-09-02 05:25:17 字数 1969 浏览 3 评论 0原文

更新:请参见下文

我有一个表: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。

列出表数据中不可用的名称和国家/地区

更新

  1. 我想使用分组来过滤邮政编码并使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

  1. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沩ん囻菔务 2024-09-09 05:25:17

试试这个。

SELECT Name, u_country, u_postcode FROM user 
WHERE u_postcode NOT IN (SELECT d_postcode FROM data)

希望有帮助

Try this.

SELECT Name, u_country, u_postcode FROM user 
WHERE u_postcode NOT IN (SELECT d_postcode FROM data)

Hope it helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文