简体   繁体   中英

SQL Select 2 colum with same name from 2 table

I have made this query:

Select type from client, store etc...

I have the "type" field in both tables, as I can only choose one of the customer table?

Put the table name in front

Select client.type 
from client
join store on ...
Select client.type, store.type,  
from client
join store on store.id = client.storeID

It is critical to always alias all columns in a query. That is simply good practice. It makes maintenance much easier.

Further, NEVER use implicit joins. THey are a SQL antipattern, they create problems with accidental cross joins, you can't tell if you really need a cross join or if it was an accident and they are harder to maintain especially if you end up with people mixing implicit inner joins and explicit outer joins which may even end up giving you incorrect results.

Use the table name. If you want type to come from table store, use:

Select store.type from client, store etc...

如果两者都需要,则必须为名称起别名:

select client.type as clienttype, store.type as storetype from client, store etc...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM