Jump to content

An interesting SQL Search problem


jonshutt

Recommended Posts

Hi folks. maybe experts here can help me out here...

 

I have 2 tables in a database, both store different information about a user.

 

eg:

table1 -> userid | name | address | phone

table2 -> userid | gender | age

 

(in fact, it's a bit more complicated than that, but that'll do for the example)

 

What I need to do is a search allong the lines of

 

SELECT * from `table1`, `table2`  where `table1.name` = 'jon' AND `table2.gender` = 'male'

 

 

However, it doesn't work, I'm getting an error like:  'Unknown column 'table1.name' in 'where clause'.

Any help appreciated.

 

If possible, it would be great to get this search query into 1 thing becuase the CMS i'm updating passes the results through an XML ajax thing, and can only display the results for one search query at a time...

 

Thanks

Link to comment
Share on other sites

I'm not sure how MySQL treats natural joins these days but I recommend reformatting your SQL to look a bit more like this:

 

SELECT *
FROM table1 t1
JOIN table2 t2 ON t1.userid = t2.userid
WHERE t1.name = 'jon'
AND t2.gender = 'male' 

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.