Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joining SQL queries with where clause

I have 2 tables in a MYSQL database wich look like this:

Klant:

ID  Naam           Email    Soort  Status
6   test           test     test2   
7   status test    test     test    20
8   soort test     test     test    
9   soort test 2   test2    Museum  

Mail:

ID  Content                 Datum       Titel 
1   (lots of encoded HTML)  18-03-13    test
2   (lots of encoded HTML)  18-03-13    test2
4   (lots of encoded HTML)  18-03-13    alles weer testen

(yes, I'm still testing alot^^)

Now I have a SQL query that selects all from 'Klant' with a where clause which gets the ID from a previous page:

$strSQL = "SELECT * FROM Klant WHERE ID =  '".$_GET["ID"]."' ";  

What I want is to JOIN this query with the following query:

SELECT ID, Titel FROM Mail; 

EDIT:

From all your answers and comments I think I begin to think my question maybe is totally wrong.. I'll explain where I need it for and I might not even need JOIN? I currently have a table wich includes the data from 'Klant' which looks like this:

enter image description here

The meaning is that I add another table which includes all the ID's and Title's from 'Mail'. I am sorry for the confusion I may have caused with you since I wasn't that clear with my question. I hope that this may clear up what I want and you guys can maybe tell me if I even need to JOIN this or can I do something else?

I am still a student and this is the first time I've had to use JOIN and I can't figure this out. If anyone can show me how to do this or push me in the right direction it would be great!

like image 414
Daanvn Avatar asked Jun 18 '26 20:06

Daanvn


1 Answers

SELECT * FROM Klant t1
JOIN
SELECT ID, Titel FROM Mail t2
ON t1.ID = t2.ID
WHERE t1.Name  = 'test'

To have the desired result do the following:

SELECT * FROM Klant t1
JOIN
SELECT ID, Titel FROM Mail t2
ON t1.ID = t2.ID

And if you want to have a specific row than just add the where clause:

WHERE t1.ID = 6

or

WHERE t1.Naam = 'test'

and so on

like image 86
Bender Avatar answered Jun 20 '26 09:06

Bender



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!