Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Multiple Selects same table

  • 21-07-2008 05:00PM
    #1
    Registered Users, Registered Users 2 Posts: 483 ✭✭


    lo,
    i have a table called properties and i am running this select statement:
    $querykk = 'SELECT `RecordID`,`Ref_Num`,`Main_Image_Path`,`County` FROM properties'
            . ' where county = "kilkenny"'
            . ' ORDER BY Date_Registered DESC'
            . ' LIMIT 1'
            . ' ';
    

    and i want to do this for a number of counties ie. dublin, cork, limerick... so my question is can i have multiple selects from the same table? then write the results to a table?

    Thanks
    Richie


Comments

  • Closed Accounts Posts: 3,357 ✭✭✭Beano


    banbutcher wrote: »
    lo,
    i have a table called properties and i am running this select statement:
    $querykk = 'SELECT `RecordID`,`Ref_Num`,`Main_Image_Path`,`County` FROM properties'
            . ' where county = "kilkenny"'
            . ' ORDER BY Date_Registered DESC'
            . ' LIMIT 1'
            . ' ';
    

    and i want to do this for a number of counties ie. dublin, cork, limerick... so my question is can i have multiple selects from the same table? then write the results to a table?

    Thanks
    Richie

    First off what flavour of SQL is this?

    I only do TSQL or PL/SQL so my answer is limited to these flavours of SQL. I would replace

    where county = "kilkenny"

    with

    where county in ("kilkenny", "dublin", "cork", "limerick")


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    What exactly do you mean by multiple selects? Do you mean a select where "kilkenny" can be any county based on user input?


  • Registered Users, Registered Users 2 Posts: 483 ✭✭banbutcher


    its mySQL, and basically what i want to do is show the latest property added to the database for each coulny in a table!

    @Mirror, i dont know if multiple selects is the right terminology for it, but i taught thats what would be needed!

    thanks again
    Richie


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Oh well If that's what you want to do, you want something like
    $query="SELECT `RecordID`,`Ref_Num`,`Main_Image_Path`, unique(`County`) FROM properties ORDER BY `your_date_reference`";
    

    This will return the most recent result for each county.


  • Registered Users, Registered Users 2 Posts: 483 ✭✭banbutcher


    thanks mirror, but if i only wanted to show results from 8 counties rather than all counties, how would i go about that?
    thanks


  • Advertisement
  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Ok, well I'm not an expert on efficiency of queries, but this should serve the purpose:
    SELECT `RecordID`,`Ref_Num`,`Main_Image_Path`,`County` max(`Date_Registered`) FROM properties' WHERE county = "kilkenny" OR county = "dublin" OR county = "carlow" etc. GROUP BY `County`
    

    This will return the max `Date_Registered` entry for each county specified in the WHERE criteria.


  • Registered Users, Registered Users 2 Posts: 483 ✭✭banbutcher


    Mirror that works Perfect, thanks a million!
    Richie


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    No problem, good luck with the project!


Advertisement