![](/img/trans.png)
[英]Selecting info from multiple tables for one unique ID with PHP and MySQL
[英]Selecting unique id in mysql
下面是我創建的表的副本。 我的目標是簡單地從第一張表中選擇唯一的id_num
,而第二張表中沒有。
我嘗試執行下面的代碼,但是以某種方式,我一直得到空結果
SELECT `first_table`.name FROM `first_table`
INNER JOIN `second_table`
ON `first_table`.id_num = `second_table`.id_num
WHERE `first_table`.name = `second_table`.name
第一表:
id_num | name
301 | Bobby
123 | George
25 | Vicky
第二張表:
id_num | name
301 | Bobby
435 | George
25 | Vicky
我想要的結果是:
id_num | name
435 | George
LEFT JOIN
應該在這里工作。
SELECT `first_table`.name FROM `first_table`
LEFT JOIN `second_table`
ON `first_table`.id_num = `second_table`.id_num
WHERE `second_table`.id_num is NULL
另請參閱此有用的信息圖
嘗試使用NOT IN
select `id_num` , name from `table2` where name not in (
SELECT t1.name FROM `table1` t1
INNER JOIN `table2` t2
ON t1.id_num = t2.id_num )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.