Results 1 to 6 of 6

Thread: [RESOLVED] SQL query for 2 tables

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    156

    Resolved [RESOLVED] SQL query for 2 tables

    I have two tables in one SQL database.
    Both have the columns firstname and lastname.
    I want to show (echo) only the names that don't appear in both.
    Can I do this with an SQL query or will I have to sort them in PHP?

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: SQL query for 2 tables

    You should be able to do that with an SQL query that you can execute from within your PHP code.

  3. #3
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: SQL query for 2 tables

    What type of SQL are you using? If it's not MySQL, you can use a full outer join, as this example depicts.

    If it's MySQL, the above method is not supported, and I'm not sure how you'd go about it. I think this question may be better suited for the Database Development section of the forum. They seem to be good at this SQL stuff...

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    156

    Re: SQL query for 2 tables

    Thanks I am using MySQL and I will query the forum link given

  5. #5
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: [RESOLVED] SQL query for 2 tables

    Quote Originally Posted by LingoOutsider View Post
    I have two tables in one SQL database.
    Both have the columns firstname and lastname.
    I want to show (echo) only the names that don't appear in both.
    Can I do this with an SQL query or will I have to sort them in PHP?
    If your criteria is only first name and last name, you need to be sure that each first name, last name combination is unique. If they are then you can get what you need from a FULL OUTER JOIN and in the case of MySql, you can use a subquery:

    Code:
    SELECT * FROM table1 WHERE firstName + lastName NOT IN (SELECT firstName + lastName FROM table2)
    
    UNION
    
    
    SELECT * FROM table2 WHERE firstName + lastName NOT IN (SELECT firstName + lastName FROM table1)
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] SQL query for 2 tables

    A full outer join is more or less equivalent to a LEFT JOIN ... UNION ... RIGHT JOIN. The RIGHT JOIN can also be a second LEFT JOIN with the table order swapped. However, if you have duplicate records in your tables, this will not give the same results as a proper full outer join.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width