Richard Tinkler
Richard Tinkler

Reputation: 1643

Do we need a JOIN for this SQL statement?

We have two 2 tables:

tbl_projekte [uid,werbemittel,projekt_name,kunden_id]

tbl_kunden [uid, kunden_name]

We are using this statement to select recordsets from tbl_projekte:

SELECT * FROM tbl_projekte WHERE werbemittel ='12' ORDER BY kunden_id ASC

How do we get the SQL statement to ORDER BY kunden_name?

Thanks for any help in advance!

Upvotes: 0

Views: 46

Answers (3)

Kami
Kami

Reputation: 1109

The ',' is an implicit natural join:

SELECT * 
FROM tbl_projekte, tbl_kunden  
WHERE werbemittel ='12' 
ORDER BY kunden_name ASC

If you want to be explicit:

SELECT * 
FROM tbl_projekte NATURAL JOIN tbl_kunden  
WHERE werbemittel ='12' 
ORDER BY kunden_name ASC

A 'natural join' merge the tables that have the same value for the primary key (uid in your case).

Upvotes: 0

juergen d
juergen d

Reputation: 204746

Yes, you need a join for this

SELECT p.* 
FROM tbl_projekte p
INNER JOIN tbl_kunden k on k.uid = p.kunden_id
WHERE p.werbemittel ='12' 
ORDER BY k.kunden_name ASC

Upvotes: 1

shadyyx
shadyyx

Reputation: 16055

If You want to order by customer name, then do it this way:

SELECT p.uid, p.werbemittel, p.projekt_name 
FROM tbl_projekte p
LEFT JOIN tbl_kunden k ON k.uid = p.kunden_id
WHERE werbemittel ='12' 
ORDER BY k.kunden_name ASC

Upvotes: 0

Related Questions