我在PHP中有这个SQL查询:
$sql="SELECT * from customer c JOIN commsone_phonelines b where b.phone_number LIKE '%".$_POST["search"]."%' AND ";
$sql.="c.company like '%".$_POST["search"]."%' OR ";
$sql.="c.forename like '%".$_POST["search"]."%' OR ";
$sql.="c.surname like '%".$_POST["search"]."%' OR ";
$sql.="CONCAT_WS(' ',c.forename, c.surname) LIKE '%".$_POST["search"]."%' OR ";
$sql.="c.phone like '%".$_POST["search"]."%' OR ";
$sql.="c.accountnumber like '%".$_POST["search"]."%' OR ";
$sql.="c.customerid like '%".$_POST["search"]."%' OR ";
$sql.="c.voip_account like '%".$_POST["search"]."%' OR ";
$sql.="REPLACE(c.postcode,' ','') LIKE '%".$_POST["search"]."%' OR ";
$sql.="REPLACE(c.postcode,'',' ') LIKE '%".$_POST["search"]."%' OR ";
$sql.="c.postcode LIKE '%".str_replace(' ','',$_POST["search"])."%' ";
$sql.=" AND c.resellerid = '' ORDER BY company ASC";
基本上,我试图从两个不同的表中进行选择,我只需要从客户表中回显信息。
如果在commsone_phonelines
表中找到了某些内容,则应该链接commsone_phonelines.customer_seq = customer.sequence
更新
我刚刚运行此查询:
SELECT c.* from customer c INNER JOIN commsone_phonelines b ON c.sequence = b.client_seq where b.phone_number LIKE '%boat%' OR c.company like '%boat%'
它返回customer表中的行是正确的,但是它显示了相同的行5次
答案 0 :(得分:1)
就个人而言,我觉得这更容易阅读...
$sql="
SELECT c.*
FROM customer c
JOIN commsone_phonelines b
ON b.customer_seq = c.sequence
WHERE b.phone_number LIKE '%{$_POST['search']}%'
AND ( c.company LIKE '%{$_POST['search']}%'
OR c.forename LIKE '%{$_POST['search']}%'
OR c.surname LIKE '%{$_POST['search']}%'
OR CONCAT_WS(' ',c.forename, c.surname) LIKE '%{$_POST['search']}%'
OR c.phone LIKE '%{$_POST['search']}%'
OR c.accountnumber LIKE '%{$_POST['search']}%'
OR c.customerid LIKE '%{$_POST['search']}%'
OR c.voip_account LIKE '%{$_POST['search']}%'
OR REPLACE(c.postcode,' ','') LIKE '%{$_POST['search']}%'
)
AND c.resellerid = ''
ORDER
BY company ASC";
答案 1 :(得分:0)
....我只需要从客户表中回复信息。
然后选择customer表的列。
$sql="SELECT c.* from customer c JOIN commsone_phonelines b where b.phone_number LIKE '%".$_POST["search"]."%' AND ";
其余查询保持不变。
修改
$sql="SELECT DISTINCT c.col1,c.col2,c.col3 from customer c JOIN commsone_phonelines b where b.phone_number LIKE '%".$_POST["search"]."%' AND ";