takn25 Posted April 4, 2011 Share Posted April 4, 2011 I have a table called data which stores name and other material and there is a table called match. Match has an auto incrementing id called match_id and a row called match_1 and match_2. Ok what happens is an id which is unique from the data table is stored in match_1 and another id which is unique from the data table is stored in match_2 For example Match_id match_1 match_2 40 1 2 40 2 1 Is there any way to retrive the data of both ids for instance in this case match_1 and match_2 based on the match_id being same? I tried a few joining queries but no luck for me could some one tell me how can I achieve this thanks. Basically get name place etc details of match 1 and match 2 based on the match_id Link to comment https://forums.phpfreaks.com/topic/232650-hi-stuck-at-this-any-way-around/ Share on other sites More sharing options...
dcro2 Posted April 4, 2011 Share Posted April 4, 2011 I'm not entirely sure, but if a field is AUTO_INCREMENT it is also unique meaning there can't be two rows with the same value in that field, right? So, you couldn't have two of the same match_id in the match table. Link to comment https://forums.phpfreaks.com/topic/232650-hi-stuck-at-this-any-way-around/#findComment-1196630 Share on other sites More sharing options...
takn25 Posted April 4, 2011 Author Share Posted April 4, 2011 Sorry that is true it was a typo! my mistake Link to comment https://forums.phpfreaks.com/topic/232650-hi-stuck-at-this-any-way-around/#findComment-1196656 Share on other sites More sharing options...
takn25 Posted April 4, 2011 Author Share Posted April 4, 2011 The thing is there is a name row in the data table and a place so if I do a while loop for fetching data I dont understand how will I tell SQL that I want to fetch data of the match_1 and also match_2 I am able to fetch data of one of these not both with a join query but I dont know how can I fetch data of both match_1 and match_2 in a single query is this possible? For instance While ($row =mysql_fetch_assoc($result)) { $name = $row['name']; echo $name } If I echo $name it just shows either match_1 or match_2 and their data not both of them I dont know if I am making any sense could some one guide me thanks alot! Link to comment https://forums.phpfreaks.com/topic/232650-hi-stuck-at-this-any-way-around/#findComment-1196658 Share on other sites More sharing options...
dcro2 Posted April 4, 2011 Share Posted April 4, 2011 I don't have any experience with JOINs or even subqueries, but this seems to work on my test database: SELECT * FROM data WHERE match_id = ANY(SELECT match_1 FROM match WHERE match_id=40) OR match_id = ANY(SELECT match_2 FROM match WHERE match_id=40) Link to comment https://forums.phpfreaks.com/topic/232650-hi-stuck-at-this-any-way-around/#findComment-1196663 Share on other sites More sharing options...
takn25 Posted April 4, 2011 Author Share Posted April 4, 2011 Thanks dcro for trying to help but its a weird case what I am trying to achieve I want to select both of the match_1 and match_2 data at the same time rather than using OR and selecting only one of these I guess its not possible but thanks for the help! Link to comment https://forums.phpfreaks.com/topic/232650-hi-stuck-at-this-any-way-around/#findComment-1196664 Share on other sites More sharing options...
dcro2 Posted April 4, 2011 Share Posted April 4, 2011 That is what my query is supposed to do.. I tested it with two rows with the same match_id and it returned the data from the data table for all the different match ids. OR just means it'll return rows that match either condition. I don't know the field names of your data table, so I just used match_id. Link to comment https://forums.phpfreaks.com/topic/232650-hi-stuck-at-this-any-way-around/#findComment-1196684 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.