| | |||||
| ||||||||
| Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics. |
![]() |
| | LinkBack | Thread Tools |
| | THREAD STARTER #1 (permalink) |
![]() Join Date: Jul 2005 Location: Coffs H, Australia
Posts: 3,456
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | |
| |
| | THREAD STARTER #3 (permalink) | ||||
![]() Join Date: Jul 2005 Location: Coffs H, Australia
Posts: 3,456
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
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.
__________________ Free Forums / GoDaddy Coupon Codes (NEW DOMAIN!) / Free Arcade Script / <?='Your computer is '.(1?fine:broken).'.'?> | ||||
| |
| | #4 (permalink) |
| NamePros Regular Join Date: Feb 2006 Location: Montreal, Quebec, Canada
Posts: 324
![]() | 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 |
| |
| | THREAD STARTER #5 (permalink) | ||||
![]() Join Date: Jul 2005 Location: Coffs H, Australia
Posts: 3,456
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
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.
__________________ Free Forums / GoDaddy Coupon Codes (NEW DOMAIN!) / Free Arcade Script / <?='Your computer is '.(1?fine:broken).'.'?> | ||||
| |
| | #6 (permalink) |
| NamePros Member Join Date: May 2006
Posts: 160
![]() | 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. |
| |
| | THREAD STARTER #7 (permalink) | ||||
![]() Join Date: Jul 2005 Location: Coffs H, Australia
Posts: 3,456
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
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.
__________________ Free Forums / GoDaddy Coupon Codes (NEW DOMAIN!) / Free Arcade Script / <?='Your computer is '.(1?fine:broken).'.'?> | ||||
| |
| | #8 (permalink) |
| Buy my domains. Join Date: Feb 2006
Posts: 2,796
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | Untested: PHP Code:
Last edited by Dan Friedman; 08-29-2006 at 10:02 AM.
|
| |
| | #11 (permalink) | ||||
| Senior Member Join Date: Jun 2003 Location: UK
Posts: 3,547
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
| ||||
| |
| | #13 (permalink) |
| New Member Join Date: Aug 2006 Location: Chelmsford, UK
Posts: 7
![]() | 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. Join Date: Feb 2006
Posts: 2,796
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
BillyConnite: tell us if that works. | ||||
| |
| | #17 (permalink) | ||||
| Domains my Dominion Join Date: Aug 2005 Location: Web 1.0
Posts: 9,552
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | Actually Adnan is close (BTW welcome to NP )Putting the rights fields together I believe we have something like (not tested):
).
__________________ NameNewsletter.com - free lists of available domain names ZoneFiles.net (beta) - ccTLD and gTLD droplists | ||||
| |
| | #18 (permalink) | ||||
| New Member Join Date: Aug 2006 Location: Chelmsford, UK
Posts: 7
![]() | 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 ![]()
| ||||
| |
| | #19 (permalink) |
| Senior Member Join Date: Oct 2005 Location: Ukraine
Posts: 1,585
![]() ![]() ![]() ![]() ![]() ![]() | 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 |
| |
| | THREAD STARTER #20 (permalink) |
![]() Join Date: Jul 2005 Location: Coffs H, Australia
Posts: 3,456
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | 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.
__________________ Free Forums / GoDaddy Coupon Codes (NEW DOMAIN!) / Free Arcade Script / <?='Your computer is '.(1?fine:broken).'.'?> |
| |
| | THREAD STARTER #21 (permalink) | ||||||||||||||||
![]() Join Date: Jul 2005 Location: Coffs H, Australia
Posts: 3,456
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
THanks alot for trying but it didnt work. I keep getting "Error near AS movie_name, (SELECT..."
,Sorry but your alterations didn't work either, I got the same error as the one above.
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.
__________________ Free Forums / GoDaddy Coupon Codes (NEW DOMAIN!) / Free Arcade Script / <?='Your computer is '.(1?fine:broken).'.'?> | ||||||||||||||||
| |
| | #23 (permalink) |
| Domains my Dominion Join Date: Aug 2005 Location: Web 1.0
Posts: 9,552
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | 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 |
| |