Get multiple data from same table with different Id [closed]

35
October 16, 2021, at 00:30 AM
Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed yesterday.

Improve this question

I have to get 9 columns say(a,b,c,d,e,f,g,h,i) from table user where id = id and 2 columns say(j,k) from same table user but from admin id. Is it possible ?

I tried UNION but it's giving me error saying columns should be same.

I also tried:

SELECT a,b,c,d,e,f,g,h,i from user where id=id JOIN select j, k from id=admin_id limit 1

But any of it is not working. how can I achieve this ?

EDIT: @Cetin Basoz has answered my doubts.

Answer 1

You could have constant values for the missing columns. ie:

SELECT a,b,c,d,e,f,g,h,i from user where id=someid 
union 
select j, k, null, null, null, null, null, null, null from user 
where id=admin_id;

EDIT: It works as suggested:

DBFiddle demo

Your question is not clear and looks like you are after a union, but maybe you were trying to say you want to get back something like:

a,b,c,d,e,f,g,h,i,j,k instead? If so, then:

select * from
(SELECT a,b,c,d,e,f,g,h,i 
from _user where id=someId) t1
cross join
(SELECT j,k 
from _user where id=admin_id) t2;
Answer 2
SELECT MAX(CASE WHEN id = @some_user_id THEN a END) AS a,
       MAX(CASE WHEN id = @some_user_id THEN b END) AS b,
--     ...
       MAX(CASE WHEN id = @some_user_id THEN i END) AS i,
       MAX(CASE WHEN id = @admin_id     THEN j END) AS j,
       MAX(CASE WHEN id = @admin_id     THEN k END) AS k
FROM source_table

If specified @user_id not exists then according column will produce NULL.

If the table is empty then the output will be empty too.

is there a way to do this any shorter way ? – naive_user

SELECT MAX(a) AS a, MAX(b) AS b, ..., MAX(k) AS k
FROM ( SELECT a,b,c,d,e,f,g,h,i,NULL AS j,NULL AS k
       FROM sourcetable 
       WHERE id = @some_user_id 
     UNION ALL
       SELECT NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,j,k
       FROM sourcetable 
       WHERE id = @admin_id ) AS subquery
READ ALSO
Jackson ObjectMapper not able to parse string as hashmap

Jackson ObjectMapper not able to parse string as hashmap

I want to parse a Json String to Map<String, Object> and use the same in the existing application

66
How to convert wasm back to C++ if I also have the original code

How to convert wasm back to C++ if I also have the original code

I'm not the best at explaining this stuff but here I go

66
Module not found: Error: Can&#39;t resolve &#39;fsevents&#39; in &#39;/var/www/html/node_modules/chokidar/lib&#39; [closed]

Module not found: Error: Can't resolve 'fsevents' in '/var/www/html/node_modules/chokidar/lib' [closed]

Want to improve this question? Add details and clarify the problem by editing this post

33
How to Inject JavaScript in Web Page to Automatically Hit Refresh Button

How to Inject JavaScript in Web Page to Automatically Hit Refresh Button

I want to update the given dataset every 1 minideally, so far I've understood the best scenario is either every 1h or every 15min from Power BI

37