0

I want to select some rows from my database from multiple tables

First table:

 Table name: ctud
 --------------------------------
 id  +   user_id   +  dep_id  
 1          1            1
 2          2            1
 3          3            2
 4          4            3

Second table:

 Table name: cdot
 -------------------------------
 id   +    username   +   name
 1          Hello         Emre
 2          Merhaba       Emma
 3          Aloha         Micheal
 4          Yup           Test

I want to take data from both tables and for this, I am using this code:

 select * 
 FROM ctud,cdot
 where ctud.user_id = cdot.username
 and ctud.user_id = 1;

but I get a blank screen.. What could be the reason for this?

1
  • ctud.user_id is the user's id, a number, cdot.username is a string, the user's name. You cannot join based on this. You need to use another table that would have both user_id and username and use that table also in your SQL. Commented Jul 21, 2014 at 18:01

2 Answers 2

1

You join your tables on this condition

ctud.user_id = cdot.username

but ctud.user_id is for instance 1 and cdot.username Hello.

That does not match and won't return results. You need 2 columns in those tables that contain the same values to make a connection of these tables.

You probably wanted to do

 select * 
 FROM ctud
 join cdot on ctud.user_id = cdot.id
 where ctud.user_id = 1;
Sign up to request clarification or add additional context in comments.

Comments

0

You only need this code

 select * 
 FROM ctud,cdot
 where ctud.user_id = cdot.id and ctud.user_id = 1;

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.