Reputation: 45
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?
Upvotes: 0
Views: 249
Reputation: 957
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
Upvotes: 0
Reputation: 330
Try this query with join:
"SELECT customer.custname,register.status,register.Desc
FROM customer
JOIN register ON register.regname = customer.custname"
Upvotes: 3
Reputation: 19764
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
.
Upvotes: 1
Reputation: 1806
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
Upvotes: 1