2014-03-02 21 views
0

兩個表的選擇有兩個表:如何從與條件

表1:

id = 0  name=JOHN   speciality =dentist 
id = 1  name=ABY   speciality= dentist 
id = 2  name = SARA   speciality= cardiologist 

,...
表2:

id = 0 name = JOHN   city=paris 
id = 1 name = ABY   city=tokio 
id = 1 name = SARA   city=london 

$city='pa'; 

$speciality='dentist'; 

(這兩個變量來自用戶已經進入的搜索表單)

我想要sa y這:

select * FROM Table 1 WHERE speciality=$speciality AND city LIKE %$city% (from Table2) ; 

我該說什麼? (很明顯,ID是兩個表的每一行相同,每個ID都代表一個人)

回答

1

嘗試這樣的:

select t1.* FROM Table 1 as t1,table2 as t2 WHERE t1.speciality='$speciality' AND t1.city LIKE %$city% and t1.name=t2.name; 

如果行ID是相同的兩個表,則:

select t1.* FROM Table 1 as t1,table2 as t2 WHERE t1.speciality='$speciality' AND t1.city LIKE %$city% and t1.name=t2.name and t1.id=t2.id ; 
+0

其不工作:( – milad