簡體   English   中英

在php mysql查詢中檢查兩個表的值

[英]Check two table value in php mysql query

我有兩張桌子。

客戶:

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

regname中的cust匹配abc and asd

然后,我要為客戶 custnname匹配的客戶名稱注冊表詳細信息。

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

如何用PHP MySQL查詢做到這一點?

您可以使用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

使用join嘗試以下查詢:

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

這是兩個表之間的簡單JOIN ,所以您想要的是:

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

由於我們使用JOIN因此它充當inner join JOIN ,並且只會返回與其匹配的值

有關mysql連接的更多信息,請參見: https : //dev.mysql.com/doc/refman/5.7/en/join.html

您可以嘗試以下代碼。

SELECT c.id as ID,c.custname as Customer Name,r.status as Status,r.desc as Description
FROM customer as c
INNER JOIN register as r
ON r.regname = c.custname

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM