简体   繁体   中英

How to use Like in left outer join

SELECT *
FROM pay_invoice_list p
LEFT outer JOIN paimentDetl d
on p.invoice_no= d.invoiceNo and p.invoice_no LIKE ' %$temp%'
ORDER BY iid DESC

This is my query. But it doesn't work correctly. The tow tables are not join properly. as well as the "Like" didn't work.

SELECT *
FROM pay_invoice_list p
LEFT outer JOIN paimentDetl d
on p.invoice_no= d.invoiceNo 
Where p.invoice_no LIKE ' %$temp%'
ORDER BY iid DESC

All you needed to do is add WHERE to your query.

Try this

SELECT *
FROM pay_invoice_list p
LEFT outer JOIN paimentDetl d
on p.invoice_no= d.invoiceNo
where p.invoice_no LIKE ' %$temp%'
ORDER BY iid DESC

You should only filter in your join statement when you want to filter the right table of your left outer join (here paimentDetl) . When you want to filter your left table (here pay_invoice_list) , you should add your filter clause in the where clause .

SELECT *
FROM pay_invoice_list p
LEFT outer JOIN paimentDetl d ON p.invoice_no= d.invoiceNo
WHERE p.invoice_no LIKE '%$temp%'
ORDER BY iid DESC

try like this

SELECT * 
FROM pay_invoice_list p 
    LEFT outer JOIN paimentDetl d ON p.invoice_no = d.invoiceNo 
WHERE p.invoice_no LIKE ' %$temp%' 
ORDER BY iid DESC

Put p.invoice_no in the WHERE clause.

SELECT *
FROM pay_invoice_list p
LEFT outer JOIN paimentDetl d on p.invoice_no= d.invoiceNo 
WHERE p.invoice_no LIKE ' %$temp%'
ORDER BY iid DESC

This will return all records from pay_invoice_list where invoice_no LIKE ' %$temp%' and all records from paimentDetl where the invoice number matches.

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