aeroswat Posted February 8, 2010 Share Posted February 8, 2010 Is there a PHP solution I would have to use here or can I do this directly through my query? I have two tables. One holds registration information for students. It has an autonumber that it assigns to each student so in turn they are distinct. Whenever a student places an order the information for the order is placed in my order table. The way that it links the two is it has the student's number on each order record. I want to do searches through the order table but I want to order it by the student's names. Quote Link to comment https://forums.phpfreaks.com/topic/191412-can-i-query-by-association/ Share on other sites More sharing options...
Mchl Posted February 8, 2010 Share Posted February 8, 2010 SELECT o.* FROM orderTable AS o INNER JOIN registrationTable AS r ON o.studentNumber = r.studentNumber WHERE r.studentName = ? Quote Link to comment https://forums.phpfreaks.com/topic/191412-can-i-query-by-association/#findComment-1009070 Share on other sites More sharing options...
aeroswat Posted February 8, 2010 Author Share Posted February 8, 2010 SELECT o.* FROM orderTable AS o INNER JOIN registrationTable AS r ON o.studentNumber = r.studentNumber WHERE r.studentName = ? So just trying to break that down real fast. I am selecting all the elements on the orderTable that can be accessed by o.columnname where the order student numbers equal the registration student numbers and only grabbing the ones where the student name from the registration equals what I want? Quote Link to comment https://forums.phpfreaks.com/topic/191412-can-i-query-by-association/#findComment-1009073 Share on other sites More sharing options...
Mchl Posted February 8, 2010 Share Posted February 8, 2010 That's how it should work. Quote Link to comment https://forums.phpfreaks.com/topic/191412-can-i-query-by-association/#findComment-1009077 Share on other sites More sharing options...
aeroswat Posted February 8, 2010 Author Share Posted February 8, 2010 That's how it should work. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/191412-can-i-query-by-association/#findComment-1009080 Share on other sites More sharing options...
aeroswat Posted February 8, 2010 Author Share Posted February 8, 2010 Now if I use inner join can i still access my column names from my original table name or do I have to put the table name in front of everything? So for instance I have this SELECT tblOrders.*, tblStudents.StudentName FROM tblOrders INNER JOIN tblStudents ON tblOrders.StudentNumber=tblStudents.StudentNumber WHERE col1='bla' AND col2='bla' ORDER BY tblStudents.StudentName If col1 and col2 are from tblOrders will I have to put tblOrders in front of them or will it be ok the way it is? Quote Link to comment https://forums.phpfreaks.com/topic/191412-can-i-query-by-association/#findComment-1009086 Share on other sites More sharing options...
Mchl Posted February 8, 2010 Share Posted February 8, 2010 You can use aliases as I did in my first post SELECT o.*, s.StudentName FROM tblOrders AS o INNER JOIN tblStudents AS s ON o.StudentNumber=s.StudentNumber WHERE s.col1='bla' AND s.col2='bla' ORDER BY s.StudentName Quote Link to comment https://forums.phpfreaks.com/topic/191412-can-i-query-by-association/#findComment-1009096 Share on other sites More sharing options...
aeroswat Posted February 8, 2010 Author Share Posted February 8, 2010 You can use aliases as I did in my first post SELECT o.*, s.StudentName FROM tblOrders AS o INNER JOIN tblStudents AS s ON o.StudentNumber=s.StudentNumber WHERE s.col1='bla' AND s.col2='bla' ORDER BY s.StudentName Alright i will. Thanks again for helping me to understand the query. Quote Link to comment https://forums.phpfreaks.com/topic/191412-can-i-query-by-association/#findComment-1009099 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.