PHP mySQL - select unique value that not being used from dirrefent table
Updates : Please see below
i have table: data
+-----------------------+--------------+-----------+
| State | d_country | d_postcode|
+-----------------------+--------------+-----------+
| State1 | Country1 | 1111 |
| State2 | Country2 | 2222 |
| State3 | Country3 | 3333 |
| State4 | Country4 开发者_如何学C | 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)
Try this.
SELECT Name, u_country, u_postcode FROM user
WHERE u_postcode NOT IN (SELECT d_postcode FROM data)
Hope it helps
精彩评论