Need help constructing a MySQL query.

  • # November 21, 2009 at 1:12 pm

    I have two tables, table1 and table2.
    I need to select all records in table1 where = ‘$name’
    and where = {
    table2.url_key = ‘google’
    table2.url_key = ‘$url’}

    do you know how this would be constructed to be a valid query?

    # November 21, 2009 at 3:09 pm

    can you give the structure of both tables and explain which values you want out of both tables?

    # November 21, 2009 at 3:12 pm

    all the fields are varchar and I want to be able to select all the values and records in table1 based on the criteria

    # November 21, 2009 at 3:19 pm

    ill give it a shot, but i kinda need more info:

    SELECT * FROM `table1`
    LEFT JOIN `table2` ON `table2`.`id` = `table1`.id`
    WHERE `table1`.`name` = ‘$name’
    AND `table2`.`url_key` = ‘google’
    AND `table2`.`url_key` = ‘$url’

    the only problem is .. how can `table2`.`url_key` be $url AND google at the same time??

    # November 22, 2009 at 8:31 am

    sorry, my fault. One of the key’s meant to be ‘value’. But anyway, your code works.


    SELECT * FROM `table1`
    LEFT JOIN `table2` ON `table2`.`id` = `table1`.id`
    WHERE `table1`.`name` = ‘$name’
    AND `table2`.`url_key` = ‘google’
    AND `table2`.`url_value` = ‘$url’
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

There's a whole bunch of content on CSS-Tricks.

Search for Stuff   •   Browse the Archives

Get the Newsletter ... or get the RSS feed