0

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?

0

4 Answers 4

3

Try this query with join:

"SELECT customer.custname,register.status,register.Desc 
 FROM customer 
 JOIN register ON register.regname = customer.custname"
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.