www.webdeveloper.com
Results 1 to 2 of 2

Thread: Need a MySQL query (this one's easy for advanced people)

  1. #1
    Join Date
    May 2009
    Location
    Rochester, NY
    Posts
    364

    Need a MySQL query (this one's easy for advanced people)

    I need a query that can display only the rows which have the correct 'color' variable in a corresponding table.

    If I have these tables:

    Code:
    Table 1:
    
    id   num  color
    1    1    yes
    2    2    yes
    3    3    yes
    4    4    no
    5    5    no
    6    6    yes
    Code:
    Table 2:
    
    id   num  answer
    1    1    345
    2    2    456
    3    3    567
    4    4    875
    5    5    342
    6    6    801
    I want to display from Table 2 all data in the 'answer' field UNLESS the corresponding 'num' in Table 1 has 'yes' in the 'color' field.

    So I want to display only:
    875
    342

    Thanks!

  2. #2
    Join Date
    Aug 2009
    Posts
    2
    Assuming that the table2.num corresponds to table1.id try the following

    SELECT t2.answer FROM table1 t1 INNER JOIN table2 t2 ON t1.id = t2.num WHERE t1.color = 'no';

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles