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
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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.