Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

I need a SQL query

  1. Feb 14, 2007 #1


    User Avatar

    I get a list from a query. Let's say "SELECT name FROM foods WHERE type='fruit';" returns "apple, orange, banana"

    I get a list from another query, "SELECT name FROM concepts WHERE type='color';" returns "red, orange, yellow"

    Now what I want is to find the common element(s) in the two lists, in this case "orange."

    What is the most efficient way to do this? SQL queries or PHP nested loops, or ???

  2. jcsd
  3. Feb 14, 2007 #2

    jim mcnamara

    User Avatar

    Staff: Mentor

    Code (Text):

    SELECT name FROM foods WHERE type='fruit'
    SELECT name FROM concepts WHERE type='color';
  4. Feb 14, 2007 #3


    User Avatar
    Homework Helper

    I would have thought of using a join:

  5. Feb 14, 2007 #4


    User Avatar

    It works well with MySQL. It will be a while before I have comparative stats but this certainly works easily. Thanks.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook