Author Topic: Can I query by association?  (Read 452 times)

0 Members and 1 Guest are viewing this topic.

Offline aeroswatTopic starter

  • Devotee
  • Posts: 848
    • View Profile
Can I query by association?
« on: February 08, 2010, 04:05:58 PM »
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.

Offline Mchl

  • Staff Alumni
  • Freak!
  • *
  • Posts: 8,582
  • Gender: Male
  • That's Largo in my avatar, not me.
    • View Profile
    • FlingBits
Re: Can I query by association?
« Reply #1 on: February 08, 2010, 04:08:05 PM »
Code: [Select]
SELECT o.* FROM orderTable AS o INNER JOIN registrationTable AS r ON o.studentNumber = r.studentNumber WHERE r.studentName = ?
NetBeans fanatic | ExtJS masochist | C++ denier
PHP4 & MySQL4 are no longer supported.
PHPFreaks Tutorials | PHP Debugging: A Beginner's guide | PHP Security Tutorial || How To Ask Questions The Smart Way
Flingbits tutorials | Class Autoloading

Offline aeroswatTopic starter

  • Devotee
  • Posts: 848
    • View Profile
Re: Can I query by association?
« Reply #2 on: February 08, 2010, 04:13:31 PM »
Code: [Select]
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?

Offline Mchl

  • Staff Alumni
  • Freak!
  • *
  • Posts: 8,582
  • Gender: Male
  • That's Largo in my avatar, not me.
    • View Profile
    • FlingBits
Re: Can I query by association?
« Reply #3 on: February 08, 2010, 04:17:08 PM »
That's how it should work.
NetBeans fanatic | ExtJS masochist | C++ denier
PHP4 & MySQL4 are no longer supported.
PHPFreaks Tutorials | PHP Debugging: A Beginner's guide | PHP Security Tutorial || How To Ask Questions The Smart Way
Flingbits tutorials | Class Autoloading

Offline aeroswatTopic starter

  • Devotee
  • Posts: 848
    • View Profile
Re: Can I query by association?
« Reply #4 on: February 08, 2010, 04:18:17 PM »

Offline aeroswatTopic starter

  • Devotee
  • Posts: 848
    • View Profile
Re: Can I query by association?
« Reply #5 on: February 08, 2010, 04:25:13 PM »
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

Code: [Select]
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?

Offline Mchl

  • Staff Alumni
  • Freak!
  • *
  • Posts: 8,582
  • Gender: Male
  • That's Largo in my avatar, not me.
    • View Profile
    • FlingBits
Re: Can I query by association?
« Reply #6 on: February 08, 2010, 04:29:57 PM »
You can use aliases as I did in my first post
Code: [Select]
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
NetBeans fanatic | ExtJS masochist | C++ denier
PHP4 & MySQL4 are no longer supported.
PHPFreaks Tutorials | PHP Debugging: A Beginner's guide | PHP Security Tutorial || How To Ask Questions The Smart Way
Flingbits tutorials | Class Autoloading

Offline aeroswatTopic starter

  • Devotee
  • Posts: 848
    • View Profile
Re: Can I query by association?
« Reply #7 on: February 08, 2010, 04:36:39 PM »
You can use aliases as I did in my first post
Code: [Select]
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.