简体   繁体   中英

MySQL only select certain row

I have this weird problem. For some reason only certain row is selected. I have this user_data table: 在此处输入图片说明

and website_name table 在此处输入图片说明

For some reason with this command using user id 13 it works:

在此处输入图片说明

but using user id 27 it didn't but you can see I add #27 in email_id row gumblar.cn.

在此处输入图片说明

Why it won't work with id 27? I'm using this command:

SELECT `email_address`, `handset` FROM `website_name` JOIN `user_data` USING ( id ) WHERE `email_id` ='27'

Any help and explanation is appreciated. Thanks in advance.

You have a coincidence that id == email_id for 13, you need to change

USING ( id )

to

ON (website_name.email_id = user_data.id)

EDIT:

In your table, you see the row:

id | website   |email_id
---+-----------+--------  
13 | yahoo.com | 13

You see how id is the same as the email_id? That is just a coincidence, and is why it worked for that number (you are joining on id, which just so happens to match the email_id, which is what you SHOULD be joining on).

Your SQL should look like:

 SELECT `email_address`, `handset` 
 FROM `website_name` 
     JOIN `user_data` ON (website_name.email_id = user_data.id) 
 WHERE `email_id` ='27';

You're joining with the id 18 and non of your users has the id 18.

You need to join it with a on statement:

join user_data on user_data.id = website_name.email_id

Both of your tables contain a field id - but, obviously, they identify different things (emails and websites, respectively). This is why your JOIN cannot use the id field to produce sensible results (that it worked for 13 is only coincidental and the query result does not make sense). You need to use

JOIN user_data ON website_name.email_id = user_data.id

A different approach would be to rename the id fields to email_id and website_id . Then, you could use JOIN user_data USING(email_id) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM