检查php mysql查询中的两个表值

时间:2018-03-13 12:49:52

标签: php mysql

我有两张桌子。

客户:

id | custname | phone
---------------------
1  | abc      | 123
2  | xyz      | 456
3  | qwe      | 786
4  | asd      | 1234

注册:

id | regname  | status  |Desc
-----------------------------------
1  | abc      | 1       | text here
2  | cvw      | 0       | text here
3  | fgr      | 1       | text here
4  | asd      | 0       | text here

cust在regname中匹配abc and asd

然后我想要为 customer register 表详细信息添加匹配custnname

id | custname | status  |Desc
-----------------------------------
1  | abc      | 1       | text here
2  | asd      | 0       | text here

如何使用PHP MySQL查询?

3 个答案:

答案 0 :(得分:2)

您可以使用INNER JOIN保留customersregisters内的所有值:

select c.id, c.custname, r.status, r.Desc
from customers c
inner join register r on r.regname = c.custname

输出:

id | custname | status  |Desc
-----------------------------------
1  | abc      | 1       | text here
4  | asd      | 0       | text here

注意:不确定您想要哪个ID。您可以使用c.idr.id

答案 1 :(得分:2)

使用join尝试此查询:

"SELECT customer.custname,register.status,register.Desc 
 FROM customer 
 JOIN register ON register.regname = customer.custname"

答案 2 :(得分:2)

这是两个表之间的简单JOIN 所以你想要的是:

SELECT customer.id,customer.custname,register.status,register.desc
FROM customer
JOIN register ON register.regname = customer.custname

因为我们使用JOIN它会充当inner join并且只会返回与之匹配的值

有关mysql join的更多信息,请查看此处:https://dev.mysql.com/doc/refman/5.7/en/join.html