c_shelswell Posted January 6, 2007 Share Posted January 6, 2007 Hi i've got a query for mysql together in php but i've had to make 2 queries to get my result when i'm sure i think i could use a join but i'm really not to sure on joins even after doing a good bit of reading.i've got 2 tables that my query joins already to get two bit of info but then i need to find one more bit of info from a 3rd table using the info from the first half of the query. Here's my code it might be easier.[code]$query = "select download_id, media_id from reg_users, purchases where reg_users.email='$email' and purchases.purch_code='$purchCode' and purchases.link_code='$seclCode'"; $result = mysql_query($query); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $details[] = $row['media_id']; } $query = "select title from media where media_id='$details[0]'"; $result = mysql_query($query); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $title[] = $row['title']; }[/code]as you can see i'm getting one result from the first query but then having to do another query to get my final bit of info. is there a better way to do this?Cheers Link to comment https://forums.phpfreaks.com/topic/33108-much-better-way-of-doing-this-with-a-join/ Share on other sites More sharing options...
weknowtheworld Posted January 6, 2007 Share Posted January 6, 2007 Hi,I think you must use a left or right join. Link to comment https://forums.phpfreaks.com/topic/33108-much-better-way-of-doing-this-with-a-join/#findComment-154306 Share on other sites More sharing options...
fenway Posted January 6, 2007 Share Posted January 6, 2007 I can re-write that as a join, but i'm not sure how you've linked purchases to reg_users in that first query. Link to comment https://forums.phpfreaks.com/topic/33108-much-better-way-of-doing-this-with-a-join/#findComment-154503 Share on other sites More sharing options...
c_shelswell Posted January 7, 2007 Author Share Posted January 7, 2007 tbh neither am i. I'm not to sure on joins and i found this in a book. It seems to give the desired results so i kept it. The query takes download_id and media_id from the Purchases table where the email matches in reg_users and purch code and link code match in the purchases table.It works but what i don't get about it is that at the start of the query i have to write "select download_id and media_id from reg_users, purchases" it's the "from reg users" bit that gets me as i actually want to select them from purchases so why do i put reg_users first?Cheers Link to comment https://forums.phpfreaks.com/topic/33108-much-better-way-of-doing-this-with-a-join/#findComment-154998 Share on other sites More sharing options...
fenway Posted January 7, 2007 Share Posted January 7, 2007 Well, you should explicitly indicate which field is from which table with a table prefix (table.column); but you're saying that there's no link between users & purchases? Link to comment https://forums.phpfreaks.com/topic/33108-much-better-way-of-doing-this-with-a-join/#findComment-155010 Share on other sites More sharing options...
c_shelswell Posted January 8, 2007 Author Share Posted January 8, 2007 sorry not sure how you mean by a link? My two tables look like this:reg_users:Username - varchar(30) pri keypassword - varchar(32)email - varchar(50)userid - varchar(32)userlevel - tinyint(1)timestamp- int(11)purchases:download_id int(10) pri keymedia_id int(10)username varchar(30)etc etcI assumed i would link them with the username? Or have i missed something? Thanks very much for your help Link to comment https://forums.phpfreaks.com/topic/33108-much-better-way-of-doing-this-with-a-join/#findComment-155714 Share on other sites More sharing options...
fenway Posted January 8, 2007 Share Posted January 8, 2007 Well, a FK constraint would be better... all I meant was that this condition isn't in your query at all! Link to comment https://forums.phpfreaks.com/topic/33108-much-better-way-of-doing-this-with-a-join/#findComment-156070 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.