简体   繁体   中英

mysql select all rows with same value in two columns

I have two tables in MySQL:

Table1:
cname
a
b
f

Table2:
character | account
a | 1
b | 2
c | 3
d | 4
e | 5
f | 6
g | 7

I want to select all accounts from two tables which cname = character

I've tried this: SELECT account FROM table1,table2 WHERE cname = character

But it returns empty. I'm sure I'm missing something simple...

Any help would be appreciated.

You need to specify your table name into query.

SELECT account FROM table1,table2 WHERE table1.cname = table2.character

OR

Use JOINS

SELECT table2.account FROM table1
LEFT JOIN table2 ON table1.cname = table2.character

Probably your data may have whitespace or any other special character appended or prepended to it. Make sure your data is fine. Better try using trim.

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