简体   繁体   中英

Check two table value in php mysql query

I have two tables.

Customer :

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

Register :

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 matches in regname : abc and asd

Then I want out put matches custnname for customer and register table details.

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

How to do it with PHP MySQL query?

You could use INNER JOIN to keep all values that are inside customers and registers :

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

Will outputs:

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

NB: not sure about which ID you want. You could use c.id or r.id .

Try this query with join:

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

this is a simple JOIN between the 2 tables so what you want is:

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

since we use JOIN it acts as an inner join and will only return values that match it

for more on mysql join look here: https://dev.mysql.com/doc/refman/5.7/en/join.html

You can try below code.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM