简体   繁体   English

SQL 服务器:连接一个表与两个表同名但 select 表中的所有数据(如果可用),否则来自第二个表

[英]SQL Server: join one table with two table have same names but select all data from table first if available, otherwise from second table

I tried searching this kind of use case on google but did found exact what i was looking so, Need help to reach to specific point by useful answer, docs or any reference我尝试在谷歌上搜索这种用例,但确实找到了我正在寻找的确切内容,需要通过有用的答案、文档或任何参考来达到特定点

I have this situation: table A has to be joined with table B and table C;我有这种情况:表 A 必须与表 B 和表 C 连接; B and C have similar columns, so there will be duplicate column names in select part, however give preferences to all the data of tables B if available otherwise show data from table c B 和 C 具有相似的列,因此在 select 部分中会有重复的列名,但是如果可用,请优先考虑表 B 的所有数据,否则显示表 Z4A8A08F09D37B737953649038408B5 中的数据

For example:例如:

SELECT 
    ae.*, ml.name as name, ml.contact AS contact,
    ve.name AS name, ve.contact AS contact
FROM 
    TABLE ae
LEFT JOIN 
    TABLE ml ON ae.eid = ml.eid
LEFT JOIN 
    TABLE ve ON ae.eid = ve.eid
WHERE
    ae.eid = 1

ml data毫升数据

eid | name | contact
----+------+--------
1   | xyz  | null

ve data数据

eid | name | contact
----+------+--------
1   | xyz  | 1

ae data数据

eid | gender
----+--------
1   | male

I want this result:我想要这个结果:

eid | gender | name | contact
----+--------+------+--------
1   | male   | xyz  | null

But I am getting this for now:但我现在得到这个:

eid | gender | name | contact | contact
----+--------+------+---------+--------
1   | male   | xyz  | 1       | null

I'm using node-mssql driver for querying SQL Server data.我正在使用 node-mssql 驱动程序来查询 SQL 服务器数据。

Thanks,谢谢,

You must join ve only if there is no matching row in ml and you do it if you add in the ON clause the condition ... AND ml.eid IS NULL .只有在ml中没有匹配的行时才必须加入ve ,并且如果在ON子句中添加条件... AND ml.eid IS NULL则必须加入。
Also use COALESCE() to select the columns from ml first and if they don't exist from ve :也使用COALESCE()到 select 首先来自ml的列,如果它们不存在于ve

SELECT ae.*, 
       COALESCE(ml.name, ve.name) AS name, 
       COALESCE(ml.contact, ve.contact) AS contact
FROM ae
LEFT JOIN ml ON ae.eid = ml.eid
LEFT JOIN ve ON ae.eid = ve.eid AND ml.eid IS NULL
WHERE ae.eid = 1

See the demo .请参阅演示
Results:结果:

eid开斋节 gender性别 name姓名 contact接触
1 1 male男性 xyz xyz null null

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL-第一个表中的所有值与第二个表中的第一个值连接在一起,否则为null - SQL - All values from first table joined with first value from second one and null otherwise 从一个表中选择ID列表,然后从SQL Server中的第二个表中删除所有ID - Select list of IDs from one table and delete all IDs from a second table in SQL Server 如何从第一个表中选择第二个表中不可用的时间? - How to select times from first table which are not available in the second table? 如何从两个表中选择作为SQL Server中的一个表 - How to SELECT from two table as one table in sql server SQL Server:使用“选择并从另一个表联接另一个表”从一个表更新 - SQL Server : update from one table with Select and Join two another table SQL(SSMS)连接一个表和第二个表中的所有记录,但从第二个表中排除“重复” - SQL (SSMS) join all records from one table and second table but exclude 'duplicates' from second SQL 服务器:如何从两个表中的 select 列显示第二个表中最接近日期的相同值 - SQL Server: How to select column from two tables and show the same value of the closest date from the second table SQL:我想从第二个表中提取数据。 由于第二个表的列名与第一个表列的逗号分隔的值匹配 - SQL: I want to extract data from second table. As column names of second table matches with values Separated by comma of first table column SQL 服务器 - 合并:匹配两次更新 - 第二次更新基于同一表中第一次更新的记录 - SQL Server - MERGE : MATCHED Two update -second update based on records from first update in same table SQL从一个表中选择一组记录,将每个记录连接到第二个表的前1个记录,该记录与第一列匹配,并按第二个表中的一列排序 - SQL Select set of records from one table, join each record to top 1 record of second table matching 1 column, sorted by a column in the second table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM