0

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

2
  • What does "OR REPLACE(c.postcode,'',' ') LIKE" do? And how might it differ from the line below it? Commented Jul 25, 2014 at 9:10
  • If you like, consider following this simple two-step course of action: 1. If you've not already done so, provide proper DDLs (and/or an sqlfiddle) so that we can more easily replicate the problem. 2. If you've not already done so, provide a desired result set that corresponds with the information provided in step 1. Commented Jul 25, 2014 at 12:16

2 Answers 2

1

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";
Sign up to request clarification or add additional context in comments.

4 Comments

doesnt help me tho "/
i can search for rows in commsone_phonelines and it returns the correct data from the customer table but i cannot search the customer table now
Ok, i just changed your JOIN to LEFT JOIN and it works fine! :)
A LEFT JOIN? But you're not selectng any columns from the RIGHT table !?!? (and "WHERE b.phone_number" renders it an INNER JOIN anyway) Still, whatever floats your boat.
0

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

11 Comments

if i search for a row in commsone_phonelines it shows the row from the customer table but multiple times. then if i search for a row in the customer table it shows nothing
do you have an example ?
see edit .. replace col1,col2,col3 nad so on with actual column names of customer table
thats working - how can i ensure if i search for a row in the commsone_phonelines it will match commsone_phonelines.customer_seq = customer.sequence ?
To get exact match use INNER JOIN
|

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.