Select from other table if value exist

╄→尐↘猪︶ㄣ 提交于 2019-12-06 12:29:49

问题


I created a fiddle for this, at this link: http://www.sqlfiddle.com/#!2/7e007

I could'nt find SQL compact / CE so it's in MySQL.

The tables looks like this

Records                      Clients
ID | NAME    | AGE           ID | NAME    
------------------           ----------------
1  | John    | 20            1  | John
2  | Steven  | 30            2  | Daniel
3  | Abraham | 30            3  |
4  | Donald  | 25            5  | Lisa 
6  |         | 35            6  | Michael
7  |         | 42            7  |

I would like to select from both tables, and if the id is in both tables and both have names I would like the the name from "Clients" as the default. If the name in Records is blank, use the client name (if any) and if the Clients.Name is blank; use the records.Name.

From tables above, i would like this:

ID | NAME    | AGE 
------------------
1  | John    | 20
2  | Daniel  | 30
3  | Abraham | 30
4  | Donald  | 25
5  | Lisa    |
6  | Michael | 35
7  |         | 42

How do i do this in SQL Compact?

EDIT: Thanks to great answers below i've managed to come up with this query, which ALMOST works:

SELECT t.id, t.name, t.age FROM 
(
    (
        SELECT r.id,
        CASE WHEN r.name = NULL THEN c.name ELSE r.name END  as name,
        r.age 
        FROM Records r 
        LEFT JOIN Clients c ON c.id = r.id
    )
    UNION 
    (
        SELECT c.id, c.name, null as age FROM Clients c where c.id NOT IN (select id from Records)
    )
) as t ORDER BY t.id 

This gives me this output:

ID | NAME    | AGE 
------------------
1  | John    | 20
2  | Daniel  | 30
3  | Abraham | 30
4  | Donald  | 25
5  | Lisa    |
6  |         | 35
7  |         | 42

"Michael" (should be on #6) is missing in this case. Why?!


回答1:


select r.id,
     IF(c.name != '',c.name,r.name) as name,
     r.age 
 FROM Records r 
 LEFT JOIN Clients c ON c.id = r.id 
 GROUP BY c.id

Use above query.

EDITED:

  SELECT t.id, t.name, t.age FROM 
(
    (
        SELECT r.id,
        CASE WHEN c.name <> '' THEN c.name ELSE r.name END  as name,
        r.age 
        FROM Records r 
        LEFT JOIN Clients c ON c.id = r.id
    )
    UNION 
    (
        SELECT c.id, c.name, null as age FROM Clients c where c.id NOT IN (select id from Records)
    )
) as t ORDER BY t.id 

Use this query.




回答2:


please try,hope this will work..

select c.id,
IF(NAME='',(select name from Records where id = c.id),'')
If(NAME=NULL,(select name from Records where id = c.id),NULL)
Else c.NAME
from client c;

cheers!!!




回答3:


    select case when a.id <> '' then a.id else b.id end as id ,
case when a.name <> '' then a.name else b.name end as name,a.age
 from records a 
full outer join clients b on a.Id = b.id
order by a.id



回答4:


Use COALECSE to get the first not-null value:

select id, coalesce(clients.name, records.name) as correct_name, records.age
from records
join clients using (id);

EDIT: In case not existing names are stored as '' instead of NULL use:

select id, case when clients.name = '' then records.name else clients.name end as correct_name, records.age
from records
join clients using (id);

Of course you can also react on both '' and NULL by asking

when clients.name = '' or clients.name is null then

See http://www.sqlfiddle.com/#!2/7e007/36.



来源:https://stackoverflow.com/questions/21305474/select-from-other-table-if-value-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!