开发者

SQL select without showing joining columns in Joins

How do I exclude dup开发者_如何学Clicate columns of joining keys when we do a join?


I assume you mean you don't want to show them in the results. If so the way is not to use select * but to explicitly list every column by name in the select then you can choose the columns you want.

This is good practice anyway as when extra columns are added or columns you don't care about change then your query returns the same results and so you do not need to alter any code reading the results.

Also as a note if you have a column in the join criteria you do not need to show it in the result.


Use an explicit column list or a NATURAL JOIN or USING (col) if your RDBMS supports this syntax. Oracle does SQL Server doesn't. (the question is tagged SQL with no particular flavour indicated)


Don't do SELECT *, instead explicitly name your columns, e.g.:

SELECT a.ID, a.AccountNumber, b.Name, b.DOB
FROM Account a
JOIN Person b on a.ID = b.ID


Use alias of the coloumns e.g:

SELECT u.id as uid, u.name as username, s.id as sid  
FROM user u
JOIN SOMETHIG s on u.id = s.user_id


It depends on the nature of your query. If you need to use the joined table as a filter then don't join it on the main query, do it as a sub-query.

SELECT *
FROM A
WHERE A.SEQ IN (SELECT B.FKEY_SEQ
                    FROM B
                    LEFT JOIN B WITH (NOLOCK) ON B.FKEY_SEQ = A.SEQ
                    WHERE B.SOMECOLUMN IN ('1234','5678'))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜