Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php mysql join to querries with limits on both

Status
Not open for further replies.

jefargrafx

Instructor
May 24, 2001
273
US
I need to return two querries with different limits on each.

Same table, one to return all proptype limit to 3 and another to reutrn all proptype that = "rental" limit to 1. to dipaly the content on one web page, only a total of 4 proptypes

can you do this in mysql

thanks

Jeff Roberts
kritterMedia.com
 
Hi

[ul]
[li]This has nothing to do with PHP. Should be asked in forum436.[/li]
[li]You not specified the condition for the first query, but I suppose you want to exclude any that may ppear in the second query, to avoid returnng duplicates.[/li]
[/ul]
Code:
[b]select[/b] [teal]*[/teal] [b]from[/b] your_table [b]where[/b] proptype[teal]!=[/teal][green][i]'rental'[/i][/green] [b]limit[/b] [purple]3[/purple]
[b]union[/b] [b]all[/b]
[teal]([/teal] [b]select[/b] [teal]*[/teal] [b]from[/b] your_table [b]where[/b] proptype[teal]=[/teal][green][i]'rental'[/i][/green] [b]limit[/b] [purple]1[/purple] [teal])[/teal]
Note that the parenthesis ( () ) around the second [tt]select[/tt] are necessary, otherwise [tt]limit[/tt] 1 would be applied to the entire statement.


Feherke.
 
yep your are right, this is really MySQL not php, but man I use a lot of MySQL in my php.

thanks for the help, you got me thinking about joins and this was the answer

$query = db_query("(SELECT * FROM property_data WHERE 1 ORDER BY rand() LIMIT 3) UNION ( SELECT * FROM property_data WHERE proptype='Rental' limit 1 )" );

thanks again

Jeff Roberts
kritterMedia.com
 
Hi

Jeff said:
man I use a lot of MySQL in my php.
Yes, but those who will come later searching Tek-Tips for problems similar to their, will search for MySQL solutions in the MySQL forum.


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top