1

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.

3
  • Now something really strange. I've tested it on SQL Fiddle, it works perfectly, but not in my real database. Additionally, SELECT * FROM table1 WHERE cname = 'a' returns an empty set... Commented Aug 26, 2013 at 6:38
  • Perhaps there are blank characters in one or other of the columns. Commented Aug 26, 2013 at 11:54
  • Note also that both you and Dipesh are wrong. character is a reserved word in MySQL. So this query won't work at all, hence why no results are returned. Commented Aug 26, 2013 at 11:59

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

7 Comments

It still returns an empty set.
@l3utterfly can you please create fiddle at SQL Fiddle so i can have better idea.
Ok, building it now. FYI, using the second query you suggested returns everything from table2
@l3utterfly so does not it that you wanted.?
Sorry, the question was wrong. I've edited it. I've built the database in SQL Fiddle and your script works perfectly... but why doesn't it work in my real database?
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.