0

Sorry if the question is too basic, I'm teaching myself TSQL and I don't know how the following is done:

I have this query:

SELECT table1.name, table2.city AS city, table2.province
FROM table1 LEFT OUTER JOIN table2
ON table1.id = table2.id
WHERE table1.name NOT LIKE 'test'
ORDER BY city DESC 

And I want to add to it this other one:

SELECT table3.country 
FROM table3 LEFT JOIN table2 
ON table3.id = table2.id
WHERE table2.city = city

So I can have this result:

name -- city -- province -- country

How can I do so?

Thanks a lot!

4

3 回答 3

0

You could do it as follows:

SELECT 
    table1.name, 
    table2.city AS city, 
    table2.province,
    table3.country
FROM 
    table1 
    LEFT OUTER JOIN table2
    ON table1.id = table2.id
    RIGHT JOIN table3
    ON table2.id = table3.id
WHERE 
    table1.name NOT LIKE 'test'
ORDER BY city DESC
于 2012-11-12T10:46:40.000 回答
0

No question is too basic! I am a SQL expert busy asking very basic Java Script questions. You may as well ask directly rather than get frustrated trying to make sense of what you can find on the web, and as long as you are making an effort then everyone is happy.

What you have asked for is in fact not basic at all and could be interpreted in many ways. A few different SQL's will give you a few different results.

Firstly, this part:

WHERE table1.name NOT LIKE 'test'

It's unusual to use LIKE without wildcard characters (%, _). If you want to exclude records that are exactly 'test' then don't use like, use this instead:

WHERE table1.name <> 'test'

<> means 'different to'

With regards, to outer joining the next table in, firstly, are you certain you want to outer join it in?

You might be after this code, although this is not exactly what you have asked for:

SELECT table1.name, table2.city AS city, table2.province
FROM table1 LEFT OUTER JOIN table2
ON table1.id = table2.id
LEFT OUTER JOIN table3
ON table1.id = table3.id
WHERE table1.name NOT LIKE 'test'
ORDER BY city DESC 

This is of course missing this bit:

AND table2.city = city

In this case are you trying to match the column city to the literal string 'city' or are you trying to match to the column city? if so, is the column in table1 or table2

There are slightly different ways of intepereting your question which in turn mean writing different SQL, which in turn gives you a different answer.

So have a think about this:

  1. Is there always a matching record in table2 against table1? if so you can use INNER JOIN instead of OUTER JOIN and save some complications
  2. You should draw a diagram of your tables with lines showing how they are connected.

Also if you give a less generic name for the tables it helps us visualise what you are trying to do.

于 2012-11-12T10:55:12.327 回答
-1

Simply use two joins in one SQL statement:

SELECT table1.name, table2.city AS city, table2.province, table3.country
FROM table1
LEFT OUTER JOIN table2 ON table1.id = table2.id
LEFT OUTER JOIN table3 ON table3.id = table2.countryid
WHERE table1.name NOT LIKE 'test' and table2.city = 'Cityname'
ORDER BY city DESC 

(Sorry guys, I didn't have time to check it, thanks for the comment. Code is revised.)

于 2012-11-12T10:48:29.650 回答