NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page SQL multiple SELECT

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 08-28-2006, 10:35 PM THREAD STARTER               #1 (permalink)
 
BillyConnite's Avatar
Join Date: Jul 2005
Location: Coffs H, Australia
Posts: 3,456
BillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond repute
 


Wildlife Parkinson's Disease Parkinson's Disease

Smile SQL multiple SELECT


Hey all,

Well I've run into a bit of trouble with SQL.

I have two tables as shown below:

CATEGORIES
id category
1 action
2 drama
3 comedy

MOVIES
id category movie
1 action Movie Name 1
2 action Movie Name 2
3 action Movie Name 3
4 action Movie Name 4
5 action Movie Name 5
6 action Movie Name 6
7 action Movie Name 7
8 action Movie Name 8
9 action Movie Name 9
10 action Movie Name 10
????: NamePros.com http://www.namepros.com/programming/232590-sql-multiple-select.html
11 comedy Movie Name 11
12 drama Movie Name 12
13 drama Movie Name 13
etc etc etc

What I would like to do is select 6 movies from each category (There are a lot more rows in my database than that) but I want to do it in one SQL query.

Anyone have some idea how?

Thanks all,
Rhett.
BillyConnite is offline  
Old 08-28-2006, 11:22 PM   #2 (permalink)
Senior Member
 
Barrucadu's Avatar
Join Date: Aug 2005
Location: East Yorkshire, England
Posts: 2,689
Barrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to behold
 




Not too sure, but just seperate your queries with semicolons.
Barrucadu is offline  
Old 08-29-2006, 01:30 AM THREAD STARTER               #3 (permalink)
 
BillyConnite's Avatar
Join Date: Jul 2005
Location: Coffs H, Australia
Posts: 3,456
BillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond repute
 


Wildlife Parkinson's Disease Parkinson's Disease
Originally Posted by Mikor
Not too sure, but just seperate your queries with semicolons.
Hey Mikor,

Thanks for your help anyway.

But i'm looking to do it in just the one query, I know this can be done in seperate queries, but i'm pretty positive there's a slightly more optimized way to do it, i've been looking at sub selects and things, but just can't get what I need.

Thanks, Rhett.
BillyConnite is offline  
Old 08-29-2006, 01:59 AM   #4 (permalink)
NamePros Regular
 
Noobie's Avatar
Join Date: Feb 2006
Location: Montreal, Quebec, Canada
Posts: 324
Noobie is on a distinguished road
 



I suggest using the category_id in the category field of your movie table instead of the actual text.
I'm not sure of the SQL for what you ask, but it can be done with 2 queries and some php
__________________
Goldkey.com is a scam
What's your BMI? | Timestamp Generator
Noobie is offline  
Old 08-29-2006, 04:02 AM THREAD STARTER               #5 (permalink)
 
BillyConnite's Avatar
Join Date: Jul 2005
Location: Coffs H, Australia
Posts: 3,456
BillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond repute
 


Wildlife Parkinson's Disease Parkinson's Disease
Originally Posted by Noobie
I suggest using the category_id in the category field of your movie table instead of the actual text.
I'm not sure of the SQL for what you ask, but it can be done with 2 queries and some php
Hey Noobie,

Yes I have used the category's id in my table, the one I placed here is simply an example. I know it can be done with multiple queries also, but I just need to know how to do it using sub selects or some other SQL coding, I'm pretty sure it can be with advanced SQL knowledge.
????: NamePros.com http://www.namepros.com/showthread.php?t=232590

Thanks anyway though,
Rhett.
BillyConnite is offline  
Old 08-29-2006, 05:54 AM   #6 (permalink)
NamePros Member
Join Date: May 2006
Posts: 160
TwistMyArm is on a distinguished road
 



Now, all assuming you're using MySQL:

To limit your request to just 6 rows, you can add 'LIMIT 6' to the end of your query.

If you want 6 random rows, then that's something different. As far as I can tell, MySQL doesn't have a native RANDOM function: there are 'hacks' out there, but they generally get slow when you reach large numbers of rows. If you want random, the best thing to do is grab them all and then get 6 randoms within PHP.
TwistMyArm is offline  
Old 08-29-2006, 07:11 AM THREAD STARTER               #7 (permalink)
 
BillyConnite's Avatar
Join Date: Jul 2005
Location: Coffs H, Australia
Posts: 3,456
BillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond repute
 


Wildlife Parkinson's Disease Parkinson's Disease
Originally Posted by TwistMyArm
Now, all assuming you're using MySQL:

To limit your request to just 6 rows, you can add 'LIMIT 6' to the end of your query.

If you want 6 random rows, then that's something different. As far as I can tell, MySQL doesn't have a native RANDOM function: there are 'hacks' out there, but they generally get slow when you reach large numbers of rows. If you want random, the best thing to do is grab them all and then get 6 randoms within PHP.
Hey TwistMyArm,

If I use the LIMIT command that will simply select a maximum of 6 games altogether, i'm looking to grab 6 games from each different category. BUT I cannot just write in each category, as this is going to be a sold script, where people can add and remove categories from the category table, this is why I need the MySQL to first get the categories from category table, and then select 6 games from each category and give them to me.
????: NamePros.com http://www.namepros.com/showthread.php?t=232590

Know what I mean?

Thanks, Rhett.
BillyConnite is offline  
Old 08-29-2006, 09:15 AM   #8 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
Join Date: Feb 2006
Posts: 2,796
Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future
 


Autism Autism Autism Autism Autism Autism Autism
Untested:
PHP Code:
$query  "SELECT * FROM MOVIES";
$result mysql_query($query);

$movies = array();

while (
$row mysql_fetch_assoc($result)) {
    if (
count($movies[$row['category']]) != 6) {
        
$movies[$row['category']][$row['id']]       = $row['id'];
        
$movies[$row['category']][$row['category']] = $row['category'];
        
$movies[$row['category']][$row['movie']]    = $row['movie'];
????: NamePros.com http://www.namepros.com/showthread.php?t=232590
    }

I'm not sure how to do it better/easier than that..
Last edited by Dan Friedman; 08-29-2006 at 10:02 AM.
Dan is offline  
Old 08-29-2006, 10:32 AM   #9 (permalink)
NamePros Regular
 
baxter's Avatar
Join Date: Apr 2006
Posts: 363
baxter is just really nicebaxter is just really nicebaxter is just really nicebaxter is just really nice
 


Ethan Allen Fund Save The Children
You could use a union select but that basically is doing multiple queries. Dan's code above is how I'd do it..
baxter is offline  
Old 08-29-2006, 11:38 AM   #10 (permalink)
NamePros Regular
 
Noobie's Avatar
Join Date: Feb 2006
Location: Montreal, Quebec, Canada
Posts: 324
Noobie is on a distinguished road
 



Any reason why it must be 1 query?

Select * categories
foreach category {
Select 6 images
}
__________________
Goldkey.com is a scam
What's your BMI? | Timestamp Generator
Noobie is offline  
Old 08-29-2006, 02:53 PM   #11 (permalink)
Senior Member
 
Scott's Avatar
Join Date: Jun 2003
Location: UK
Posts: 3,547
Scott has a reputation beyond reputeScott has a reputation beyond reputeScott has a reputation beyond reputeScott has a reputation beyond reputeScott has a reputation beyond reputeScott has a reputation beyond reputeScott has a reputation beyond reputeScott has a reputation beyond reputeScott has a reputation beyond reputeScott has a reputation beyond reputeScott has a reputation beyond repute
 

Member of the Month
February 2005

Originally Posted by Noobie
Any reason why it must be 1 query?
????: NamePros.com http://www.namepros.com/showthread.php?t=232590

Select * categories
foreach category {
Select 6 images
}
If there were 30 categories that would be a lot of queries.
Scott is offline  
Old 08-29-2006, 03:09 PM   #12 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
Join Date: Feb 2006
Posts: 2,796
Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future
 


Autism Autism Autism Autism Autism Autism Autism
Originally Posted by Scott
If there were 30 categories that would be a lot of queries.
Caching
Dan is offline  
Old 08-29-2006, 03:18 PM   #13 (permalink)
New Member
 
DodgyV's Avatar
Join Date: Aug 2006
Location: Chelmsford, UK
Posts: 7
DodgyV is an unknown quantity at this point
 



Assuming you are using MySQL, I think this is what you are looking for

SELECT
movies.id AS movie_id,
movies.name AS movie_name,
(
SELECT
categories.category
FROM
categories
WHERE
categories.category = movies.category
ORDER BY RAND() LIMIT 6
) AS movie_category
FROM
movies

Hope this helps
DodgyV is offline  
Old 08-29-2006, 03:56 PM   #14 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
Join Date: Feb 2006
Posts: 2,796
Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future
 


Autism Autism Autism Autism Autism Autism Autism
Originally Posted by DodgyV
Assuming you are using MySQL, I think this is what you are looking for
????: NamePros.com http://www.namepros.com/showthread.php?t=232590

Code:
SELECT
	movies.id AS movie_id,
	movies.name AS movie_name,
	(
		SELECT
			categories.category
		FROM
			categories
		WHERE
			categories.category = movies.category
		ORDER BY RAND() LIMIT 6
	) AS movie_category
FROM
	movies
Hope this helps
Wow at that query..
BillyConnite: tell us if that works.
Dan is offline  
Old 08-29-2006, 04:04 PM   #15 (permalink)
NamePros Regular
 
Noobie's Avatar
Join Date: Feb 2006
Location: Montreal, Quebec, Canada
Posts: 324
Noobie is on a distinguished road
 



i think u need 4.1 but props to that for sure

and is that ca-ching or caching ...
__________________
Goldkey.com is a scam
What's your BMI? | Timestamp Generator
Noobie is offline  
Old 08-29-2006, 04:15 PM   #16 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
Join Date: Feb 2006
Posts: 2,796
Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future
 


Autism Autism Autism Autism Autism Autism Autism
Caching. Like cache-ing.
Dan is offline  
Old 08-29-2006, 04:28 PM   #17 (permalink)
Domains my Dominion
 
sdsinc's Avatar
Join Date: Aug 2005
Location: Web 1.0
Posts: 9,552
sdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatness
 


Third World Education Find Marrow Donors! Find Marrow Donors! Find Marrow Donors! Find Marrow Donors! Animal Rescue Animal Cruelty AIDS/HIV Animal Rescue Wildlife Breast Cancer Animal Rescue Wildlife
Actually Adnan is close (BTW welcome to NP )
Putting the rights fields together I believe we have something like (not tested):

Quote:
SELECT movies.id AS movie_id, movies.movie AS movie_name, (
SELECT categories.category
FROM categories
WHERE categories.id = movies.category
LIMIT 6
) AS movie_category
FROM movies
ORDER BY RAND( ) will pickup records in random order (if desired ).
__________________
NameNewsletter.com - free lists of available domain names
ZoneFiles.net (beta) - ccTLD and gTLD droplists
sdsinc is offline  
Old 08-29-2006, 04:40 PM   #18 (permalink)
New Member
 
DodgyV's Avatar
Join Date: Aug 2006
Location: Chelmsford, UK
Posts: 7
DodgyV is an unknown quantity at this point
 



I wasn't quite sure which 6 movies from the list were wanted, so I figured showing 6 random ones would give the general idea of how the query works

Originally Posted by sdsinc
Actually Adnan is close (BTW welcome to NP )
????: NamePros.com http://www.namepros.com/showthread.php?t=232590
Putting the rights fields together I believe we have something like (not tested):



ORDER BY RAND( ) will pickup records in random order (if desired ).
DodgyV is offline  
Old 08-29-2006, 06:06 PM   #19 (permalink)
Senior Member
 
wasistdas's Avatar
Join Date: Oct 2005
Location: Ukraine
Posts: 1,585
wasistdas is a name known to allwasistdas is a name known to allwasistdas is a name known to allwasistdas is a name known to allwasistdas is a name known to allwasistdas is a name known to all
 



I dont know how it is in PHP. In ColdFusion it is quite easy with QoQ - Query of Query. You select all records you need and then just select from the selection made.
__________________
The Official Dead End Place: WWW.TODEP.COM. The best search engine ever. WWW.NULLSEARCH.COM
For Sale: EnjoyArcade.com, Arcadoid.com
wasistdas is offline  
Old 08-31-2006, 07:26 PM THREAD STARTER               #20 (permalink)
 
BillyConnite's Avatar
Join Date: Jul 2005
Location: Coffs H, Australia
Posts: 3,456
BillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond repute
 


Wildlife Parkinson's Disease Parkinson's Disease
Hey all!

Thanks for all your suggestions, I've been busy lately and haven't been able to try these out yet, but I'll let you know how they all go.

I'll have a chance to thank you all later .

Thanks again,
Rhett.
BillyConnite is offline  
Old 09-02-2006, 01:48 AM THREAD STARTER               #21 (permalink)
 
BillyConnite's Avatar
Join Date: Jul 2005
Location: Coffs H, Australia
Posts: 3,456
BillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond repute
 


Wildlife Parkinson's Disease Parkinson's Disease
Originally Posted by DodgyV
Assuming you are using MySQL, I think this is what you are looking for

SELECT
movies.id AS movie_id,
movies.name AS movie_name,
(
SELECT
categories.category
FROM
categories
WHERE
categories.category = movies.category
ORDER BY RAND() LIMIT 6
) AS movie_category
FROM
movies

Hope this helps
Hey DodgyV,

THanks alot for trying but it didnt work. I keep getting "Error near AS movie_name, (SELECT..."
Originally Posted by sdsinc
Actually Adnan is close (BTW welcome to NP )
Putting the rights fields together I believe we have something like (not tested):
????: NamePros.com http://www.namepros.com/showthread.php?t=232590

Quote:
SELECT movies.id AS movie_id, movies.movie AS movie_name, (
SELECT categories.category
FROM categories
WHERE categories.id = movies.category
LIMIT 6
) AS movie_category
FROM movies
ORDER BY RAND( ) will pickup records in random order (if desired ).
Hey sdsinc ,

Sorry but your alterations didn't work either, I got the same error as the one above.
Originally Posted by Dan Friedman
Untested:
PHP Code:
$query  "SELECT * FROM MOVIES";
$result mysql_query($query);

$movies = array();

while (
$row mysql_fetch_assoc($result)) {
    if (
count($movies[$row['category']]) != 6) {
        
$movies[$row['category']][$row['id']]       = $row['id'];
????: NamePros.com http://www.namepros.com/showthread.php?t=232590
        
$movies[$row['category']][$row['category']] = $row['category'];
        
$movies[$row['category']][$row['movie']]    = $row['movie'];
    }

I'm not sure how to do it better/easier than that..
Hey Dan,

I would've liked to do it all throug SQL to leave the script just that bit more optimized, but I think I'll have to do it through php.

Thanks everyone for your help, and rep left for all.

Rhett.
BillyConnite is offline  
Old 09-02-2006, 02:51 AM   #22 (permalink)
Senior Member
Join Date: May 2003
Posts: 2,187
adam_uk is a jewel in the roughadam_uk is a jewel in the roughadam_uk is a jewel in the rough
 


Breast Cancer
take a look at SQL joins

http://www.w3schools.com/sql/sql_join.asp
adam_uk is offline  
Old 09-02-2006, 12:49 PM   #23 (permalink)
Domains my Dominion
 
sdsinc's Avatar
Join Date: Aug 2005
Location: Web 1.0
Posts: 9,552
sdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatness
 


Third World Education Find Marrow Donors! Find Marrow Donors! Find Marrow Donors! Find Marrow Donors! Animal Rescue Animal Cruelty AIDS/HIV Animal Rescue Wildlife Breast Cancer Animal Rescue Wildlife
Rhett I assume the relationship between the two tables is
categories.id = movies.category
and that both fields are INT right ?
Perhaps you could post the table structure ?
__________________
NameNewsletter.com - free lists of available domain names
ZoneFiles.net (beta) - ccTLD and gTLD droplists
sdsinc is offline  
Old 09-03-2006, 11:30 AM   #24 (permalink)
Account Closed
Join Date: Oct 2005
Location: India
Posts: 299
alanpaladka is on a distinguished road
 



Have u tried table join sql queries?
alanpaladka is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 06:50 PM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger