CloudNine Posted October 16, 2008 Share Posted October 16, 2008 I'm working with a MySQL database table that looks, basically, like this... | fid | uid | value | |------------------------------| | 1 | 1 | Bob | | 2 | 1 | Jones | | 3 | 1 | 102356 | | 1 | 2 | Todd | | 2 | 2 | Smith | | 3 | 2 | 652891 | |------------------------------| The 'fid' column refers to a number assigned to a custom user profile field. In this example, the value in the row with the fid of 1 corresponds to a First Name field, the value in the row with the fid of 2 to a Last Name field, and the fid of 3 to, let's say, an employee ID number. The 'pid' column denotes the ID number of the profile that this information corresponds to. In this case the pid or profile ID of 1 holds all the values relevant to Bob Jones, and the pid of 2 holds Todd Smith's values. MY QUESTION: What is the best query structure/code for obtaining a value or value(s) if I have a particular profile ID in mind and I want to say map the value in the row with pid=2 and fid=1 to a firstName variable, pid=2 and fid=2 to lastName, and pid=2 and fid=3 to employID? Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/128692-solved-query-structure-question/ Share on other sites More sharing options...
fenway Posted October 16, 2008 Share Posted October 16, 2008 You're looking to make a pivot table... data stored in this way isn't not really easily coerced into other formats. Quote Link to comment https://forums.phpfreaks.com/topic/128692-solved-query-structure-question/#findComment-667133 Share on other sites More sharing options...
CloudNine Posted October 17, 2008 Author Share Posted October 17, 2008 A pivot table, excellent, I will look into this further. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/128692-solved-query-structure-question/#findComment-667873 Share on other sites More sharing options...
Barand Posted October 17, 2008 Share Posted October 17, 2008 try SELECT a.fvalue as fname, b.fvalue as lname, c.fvalue as empID FROM user2 a JOIN user2 b USING (uid) JOIN user2 c USING (uid) WHERE a.uid = 2 AND a.fid = 1 AND b.fid = 2 AND c.fid = 3 --> [pre]+-------+-------+--------+ | fname | lname | empID | +-------+-------+--------+ | Todd | Smith | 652891 | +-------+-------+--------+[/pre] Quote Link to comment https://forums.phpfreaks.com/topic/128692-solved-query-structure-question/#findComment-668254 Share on other sites More sharing options...
fenway Posted October 17, 2008 Share Posted October 17, 2008 Using CROSS JOIN makes the purpose of the JOIN explicit.... Quote Link to comment https://forums.phpfreaks.com/topic/128692-solved-query-structure-question/#findComment-668267 Share on other sites More sharing options...
Barand Posted October 17, 2008 Share Posted October 17, 2008 Shoudn't a CROSS JOIN be used when there are no join criteria (ie a cartesian join) otherwise it's just an (INNER) JOIN EDIT: In answer to that question, std SQL Yes. In MySQL' date=' CROSS JOIN is a syntactic equivalent to INNER JOIN (they can replace each other). In standard SQL, they are not equivalent. INNER JOIN is used with an ON clause, CROSS JOIN is used otherwise. [/quote'] Quote Link to comment https://forums.phpfreaks.com/topic/128692-solved-query-structure-question/#findComment-668270 Share on other sites More sharing options...
fenway Posted October 17, 2008 Share Posted October 17, 2008 I never used "JOIN" as a bareword... it's always INNER, LEFT, or CROSS. The first two require (or should, IMHO) an ON clause -- so I expect to see one right away. The last one doesn't -- I use it when trying to find multiple matches in a many-to-many table -- so I use CROSS JOIN to indicate that the specified value will be in the where clause. Basically, my rule is like this: if the "join" compares two DB fields, it goes in an ON clause; otherwise, it goes in a WHERE clause while referencing a constant (e.g. for CROSS JOIN). Obviously, LEFT JOIN is sometimes an exception. Besides, why not stick to standard SQL whenever possible? Quote Link to comment https://forums.phpfreaks.com/topic/128692-solved-query-structure-question/#findComment-668321 Share on other sites More sharing options...
Barand Posted October 17, 2008 Share Posted October 17, 2008 I'll agree I should probably have specified INNER JOIN (but then again I never specify OUTER in LEFT OUTER JOINS) but they certainly aren't (standard sql) CROSS JOINs Quote Link to comment https://forums.phpfreaks.com/topic/128692-solved-query-structure-question/#findComment-668324 Share on other sites More sharing options...
fenway Posted October 17, 2008 Share Posted October 17, 2008 To each his own... but if I'm not using an ON clause, then I like to be explicit so that I don't think it's a mistake. Quote Link to comment https://forums.phpfreaks.com/topic/128692-solved-query-structure-question/#findComment-668338 Share on other sites More sharing options...
Barand Posted October 17, 2008 Share Posted October 17, 2008 But they are using ON clauses "USING(X)" is identical to "ON a.X = b.X" Quote Link to comment https://forums.phpfreaks.com/topic/128692-solved-query-structure-question/#findComment-668351 Share on other sites More sharing options...
fenway Posted October 18, 2008 Share Posted October 18, 2008 Sorry, I entirely missed that when I read the query... my bad. When there are const join conditions, my brain has issues. Generally, I write those as follows to make my brain hurt less: SELECT a.fvalue as fname, b.fvalue as lname, c.fvalue as empID FROM user2 AS a JOIN user2 AS b ON (b.uid = a.uid AND b.fid = 2 ) JOIN user2 AS c ON (c.uid = a.uid AND c.fid = 3 ) WHERE a.fid = 1 AND a.uid = 2 Quote Link to comment https://forums.phpfreaks.com/topic/128692-solved-query-structure-question/#findComment-668697 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.