[英]how to use inner join in my condition
我有兩個這樣的桌子
c_ID name
--- ------
7 a
6 a
5 a
4 d
和
c_ID photo
----- ------
7 1
6 1
5 0
4 1
如何選擇名稱為a
且照片為1
?
謝謝
select * /*TODO: Add specific column(s) you want here*/
from table1
join table2
on table1.c_ID = table2.c_ID
where table1.name = 'a'
and table2.photo = 1
SELECT * FROM table1 AS name LEFT JOIN table2 AS photo ON name.c_ID = photo.c_ID WHERE name.name = 'a' and photo.photo = 1
話雖這么說,您的示例看起來可能可以將兩個表歸一化為一個包含c_ID,名稱和照片字段的表
嘗試這個:
SELECT table1.c_id, table1.name, table2.photo
FROM table1 INNER JOIN table2
ON table1.c_id = table2.c_id
AND table1.name = 'a'
AND table2.photo = 1
SELECT t1.*, t2.*
FROM table1 t1
JOIN table2 t2 ON t2.c_ID = t1.c_ID
WHERE t1.name = 'a' AND t2.photo = 1
最好不要在數據庫中使用大寫字符。
SELECT table1.c_ID, table1.name, table2.photo // desired fields
FROM table1 INNER JOIN table2 ON table1.c_ID=table2.c_ID // joining tables on common keys
WHERE table1.name='a' AND table2.photo=1; // desired condition
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.