SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    seattle
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    select data from 1 table based on criteria from another table

    is it possible to select all the data in one table based on a criteria from another table?

    for instance i want to select all the therapist from massage_therapist WHERE massage_schedule.finish > 0.
    i don't want merged results. i just need to list all therapist based on the where criteria from a different table.

    these two tables have the therapist_id in common.

  2. #2
    SitePoint Member
    Join Date
    May 2008
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    SELECT * FROM message_therapist WHERE therapist_id IN (SELECT therapist_id FROM table2 WHERE message_scedule.finish > 0)

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,015
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    Code:
    SELECT message_therapist.* 
      FROM message_therapist 
    INNER
      JOIN massage_schedule
        ON massage_schedule.therapist_id = message_therapist.therapist_id 
       AND massage_schedule.finish > 0
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #4
    SitePoint Member
    Join Date
    May 2008
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Which one will be more efficient?

    I would believe mine is, - but I don't really know much about MySQL, - but making the join will make a big internal table, - that really isn't needed. Will it not?

  5. #5
    SitePoint Member
    Join Date
    May 2008
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mine with right syntax will be something like

    Code:
    SELECT *
    FROM message_therapist
    WHERE therapist_id IN
      (SELECT therapist_id
       FROM message_schedule
       WHERE finish > 0)

  6. #6
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,015
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by dadane View Post
    but making the join will make a big internal table, - that really isn't needed. Will it not?
    that is correct, it will not

    do an EXPLAIN on both queries to see how the optimizer handles them

    many times, a query with a subquery is actually converted into a join

    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  7. #7
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    seattle
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks dadane...works perfectly.


Bookmarks

Posting Permissions

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