1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

MySQL CONCAT, HAVING and JOIN

Discussion in 'Databases' started by neilfurry, Jul 9, 2016.

  1. #1
    hi can this be use all together?

    here is my query:

    SELECT CONCAT(fullname," ",address1," ",city," ",state," ",zip," ",firstname," ",lastname," ",email," ",hearaboutus," ",homephone," ",workphone," ",cellphone) AS searchterm

    FROM referrals

    INNER JOIN address ON address.clientid=referrals.clientid

    WHERE referrals.firstname is not null AND address.prim=1

    HAVING searchterm LIKE '%{$term}%'

    On the console im getting the error "[Err] 1054 - Unknown column 'address1' in 'field list'"

    this means that the address table is not recognized using JOIN

    Can you help me with this?

    Thank you
     
    neilfurry, Jul 9, 2016 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #2
    We probably need to have a separate conversation about why you are using concat
    I suspect (tell me if I'm wrong) that it's actually complaining that it doesn't know what you are referring to when you say "address1" as it's in both the referrals and address tables?

    Lets rework your query. You want to know if $term is in any of those fields

    $sql = "SELECT referral.id
    FROM referrals
    INNER JOIN address ON address.clientid = referrals.clientid
    WHERE referrals.firstname IS NOT NULL
    AND address.prim = 1
    AND (referral.fullname LIKE '%{$term}%'
        OR referral.firstname LIKE '%{$term}%'
        OR referral.lastname LIKE '%{$term}%'
        OR referral.email LIKE '%{$term}%'
        OR referral.hearaboutus LIKE '%{$term}%'
        OR address.address1  LIKE '%{$term}%'
        OR address.city LIKE '%{$term}%'
        OR address.state LIKE '%{$term}%'
        OR address.zip LIKE '%{$term}%'
        OR address.homephone LIKE '%{$term}%'
        OR address.workphone LIKE '%{$term}%'
        OR address.cellphone LIKE '%{$term}%'
        )";
    Code (markup):
    and then you can do funky stuff in the order by to get the best match to be at the top of the list.

    Now, in my code, I've split out all the tests and I've got a similar thing on one of my client sites - but I don't have all of them as "like" - columns like email I insist on being given in full. I'd do the same for phone numbers too. That might change your query to this which would be faster:

    $sql = "SELECT referral.id
    FROM referrals
    INNER JOIN address ON address.clientid = referrals.clientid
    WHERE referrals.firstname IS NOT NULL
    AND address.prim = 1
    AND (referral.fullname LIKE '%{$term}%'
        OR referral.firstname LIKE '%{$term}%'
        OR referral.lastname LIKE '%{$term}%'
        OR referral.email = '{$term}'
        OR referral.hearaboutus LIKE '%{$term}%'
        OR address.address1  LIKE '%{$term}%'
        OR address.city LIKE '%{$term}%'
        OR address.state LIKE '%{$term}%'
        OR address.zip LIKE '%{$term}%'
        OR address.homephone = '{$term}'
        OR address.workphone = '{$term}'
        OR address.cellphone= '{$term}'
        )";
    Code (markup):
    I just hauled out some code for you as an example. Each time I do a test I'm giving the result a value. If the name is an exact match they get 4 points. If it's an inexact match they get 2 points. If the person's name matches they get another 2 points and if the address is an exact match they get 2 more, the same with email. Now, you can use your own rules to decide how many points and for what but if you are in a situation where there might be more than one record returned then "weighting" is a really good idea.

    select Membership.name, 
    CASE
             WHEN `Membership`.`name` = "smith"
             THEN 4
             ELSE 0
          END +
          CASE
             WHEN `Membership`.`name` LIKE "%smith%"
             THEN 2
             ELSE 0
          END +
          CASE
             WHEN `Individual`.`lastname` LIKE "%smith%"
             THEN 2
             ELSE 0
          END +
          CASE
             WHEN `Membership`.`postal1` = '4 oaktree lane'
             THEN 2
             ELSE 0
          END +
          CASE
             WHEN `Individual`.`email` = 'smith@myname.com'
             THEN 4
             ELSE 0
          END
       ) AS `relevance`
    from Membership
    left join Individual on Membership.id = Individual.membershipId
    where ...
    order by relevance
    Code (markup):
     
    Last edited: Jul 10, 2016
    sarahk, Jul 10, 2016 IP
  3. neilfurry

    neilfurry Active Member

    Messages:
    55
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    63
    #3
    Hello,

    im using concat, join and HAVING because i want to search from 2 different tables from a keyword sent via form using Autocomplete.

    so when i type any name or address or phone number on the autocomplete field it will search through the address and referrals table with that keyword.

    On the other hand im using "HAVING" because of the alias a set on the CONCAT statement.

    Thank you.
     
    neilfurry, Jul 10, 2016 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #4
    Autocomplete should just be calling a file eg /memberships/getnames/ and you should be able to work the query however you like.

    Make sure you reference the table names in your query.
     
    sarahk, Jul 10, 2016 IP