Jump to content

Newbie tricky SQL statement


thedepotnetwork

Recommended Posts

I am trying to figure this out, and it is stumping me... even after google searches...

 

I simply want to pull data from two tables, one contains all the data I need, but the VENDOR STATE "vendor_state" is stored as a foreign key so, i simply need to get the state name 2 char "state" from the states table all in one select statement...

 

Here is what i  have so far..

 

 SELECT concat( v.vendor_firstname, " ", v.vendor_lastname ) , v.vendor_title, v.vendor_address1, v.vendor_city, s.state, v.vendor_zip, concat( v.vendor_contact_phone1, v.vendor_contact_phone2, v.vendor_contact_phone3 ) , v.vendor_email
FROM vendors AS v, states AS s
WHERE v.vendor_state =3

 

This obviously won't work, but just returns the first listed states "ie. AL" when id 3 is Arizona.

Link to comment
https://forums.phpfreaks.com/topic/190557-newbie-tricky-sql-statement/
Share on other sites

Hi

 

What you have there will bring back every possible combination of the rows from the 2 tables where v.vendor_state is 3 (it is a CROSS JOIN, and they can be useful).

 

What you need is something like this

 

SELECT concat( v.vendor_firstname, " ", v.vendor_lastname ) , v.vendor_title, v.vendor_address1, v.vendor_city, s.state, v.vendor_zip, concat( v.vendor_contact_phone1, v.vendor_contact_phone2, v.vendor_contact_phone3 ) , v.vendor_email
FROM vendors v
INNER JOIN states s
ON v.vendor_state = s.vendor_state
WHERE v.vendor_state =3 

 

All the best

 

Keith

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.