| |||||||
| Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics. |
![]() |
| | LinkBack | Thread Tools |
| | #1 (permalink) |
![]() | 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 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.
__________________ <?php if(1===1){ $computer="fine."; }else{ $computer="broken."; } echo "Your computer is ".$computer; ?> |
| |
| | #3 (permalink) | |
![]() | Quote:
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.
__________________ <?php if(1===1){ $computer="fine."; }else{ $computer="broken."; } echo "Your computer is ".$computer; ?> | |
| |
| | #5 (permalink) | |
![]() | Quote:
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. Thanks anyway though, Rhett.
__________________ <?php if(1===1){ $computer="fine."; }else{ $computer="broken."; } echo "Your computer is ".$computer; ?> | |
| |
| | #6 (permalink) |
| NamePros Member | 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. |
| |
| | #7 (permalink) | |
![]() | Quote:
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. Know what I mean? Thanks, Rhett.
__________________ <?php if(1===1){ $computer="fine."; }else{ $computer="broken."; } echo "Your computer is ".$computer; ?> | |
| |
| | #8 (permalink) |
| Buy my domains. | Untested: PHP Code: Last edited by Dan Friedman; 08-29-2006 at 09:02 AM. |
| |
| | #13 (permalink) |
| New Member | 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 |
| |
| | #14 (permalink) | |
| Buy my domains. | Quote:
BillyConnite: tell us if that works. | |
| |
| | #17 (permalink) | |
| Domains my Dominion | Actually Adnan is close (BTW welcome to NP )Putting the rights fields together I believe we have something like (not tested): Quote:
).
__________________ Buy now - MassDeveloper.com $500 | |
| |
| | #18 (permalink) | |
| New Member | 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 ![]() Quote:
| |
| |
| | #19 (permalink) |
| Senior Member | 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: Loan.IM, EnjoyArcade.com, Arcadoid.com |
| |
| | #20 (permalink) |
![]() | 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.
__________________ <?php if(1===1){ $computer="fine."; }else{ $computer="broken."; } echo "Your computer is ".$computer; ?> |
| |
| | #21 (permalink) | ||||
![]() | Quote:
THanks alot for trying but it didnt work. I keep getting "Error near AS movie_name, (SELECT..." Quote:
,Sorry but your alterations didn't work either, I got the same error as the one above. Quote:
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.
__________________ <?php if(1===1){ $computer="fine."; }else{ $computer="broken."; } echo "Your computer is ".$computer; ?> | ||||
| |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| |