gpt4 book ai didi

php - 使用 JOIN 从两个表中进行选择的 SQL 查询

转载 作者:行者123 更新时间:2023-11-29 12:49:42 25 4
gpt4 key购买 nike

我在 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%'

它从客户表中返回行,这是正确的,但它显示同一行 5 次

最佳答案

就我个人而言,我发现这更容易阅读......

 $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";

关于php - 使用 JOIN 从两个表中进行选择的 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24951284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com