简体   繁体   中英

SQL Query using a JOIN to select from two tables

I have this SQL Query in PHP:

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

i am basically, trying to select from two different tables and i need to echo information only from the customer table.

If something is found in the commsone_phonelines table, it should link commsone_phonelines.customer_seq = customer.sequence

UPDATE:

I have just run this Query:

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

it is returning rows from the customer table which is correct, however it shows the same row 5 times

Personally, I find this easier to read...

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

....I need to echo information only from the customer table.

Then select columns of customer table only.

$sql="SELECT c.* from customer c JOIN commsone_phonelines b where b.phone_number LIKE '%".$_POST["search"]."%' AND ";

Rest of query remains same.

Edit

 $sql="SELECT DISTINCT c.col1,c.col2,c.col3 from customer c JOIN commsone_phonelines b where b.phone_number LIKE '%".$_POST["search"]."%' AND ";

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