php - 如何从两个有条件的表中进行选择

标签 php mysql

有两个表:

表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';

(这两个变量来自用户输入的搜索表单)

我想说的是:

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

我该说什么? (很明显,两个表的每一行的id都是相同的,每个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;

如果两个表的行 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 ;

关于php - 如何从两个有条件的表中进行选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22134298/

相关文章:

php - 结合 Angularjs 和 CodeIgniter

php - 不同的 PHP 压缩库有什么好处?

c# - 将 'bitmap' 从 C# 上传到 PHP

php - 插入时出错

php - MySQL 匹配和不匹配组合

MYSQL JOIN语法如何连接三张表

php - 循环检查表中是否存在并生成随机字符串

php - PDOStatement::fetch 偶尔会返回 false,但 errorInfo() 为空

php - 显示来自数据库的 DD/MM/YYYY PHP 回显

php - mySQL 删除时出现意外标记