sql query

Discussion in 'MySQL' started by kabucek, Oct 14, 2008.

  1. #1
    hello @LL

    is it possible to make a query like this?

    - providing names of table1 & table2.

    - display data from table1 where userID in table1 is the same as userID in table2 ?


    is this possible ?

    Thanks
     
    kabucek, Oct 14, 2008 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    You can use a join to do this easily.

    Assuming you're using mysql.

    SELECT * FROM table1 INNER JOIN table2 ON table1.userID = table2.userID;

    If you are using a different database, the syntax may be different. Also if you need to select specific columns you need to format it like this.

    SELECT table1.name, table2.address, etc... FROM table1 INNER JOIN table2 ON table1.userID = table2.userID;
     
    jestep, Oct 14, 2008 IP
  3. kabucek

    kabucek Guest

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks i got this by using this:

    SELECT  `first`, `last`, `street1`, `street2`, `city`, `state`, `zip`, `emailAdr` FROM `table1`, `table2` WHERE table1.userID = table2.userID
    Code (markup):

    what about if I want to do another query based on the
    result of that one?

    is it possible?
     
    kabucek, Oct 14, 2008 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    Can you explain what you are trying to accomplish with a second query based on the results from the above query? It may be possible, it just depends on what you are trying to do.

    Also, you should specify the table name for the specific column you are retrieving. This makes selecting from joins quicker. Also, if more than one table has a column with the same name, you will get an ambiguous error when querying with a join.

    Use `table1`.`first` instead of `first`
     
    jestep, Oct 14, 2008 IP