Lim Neo
Lim Neo

Reputation: 75

SQL to make new column(mySQL)

I got two sql statements:

SELECT username AS userA FROM WHERE username='abc'
SELECT username AS userB FROM WHERE username='abcd' AND password='abcd'

I want to output the result as below:

userA  |  userB
----------------
abc    |  abcd

How to combine two SQL statements?

Upvotes: 1

Views: 46

Answers (4)

uh_big_mike_boi
uh_big_mike_boi

Reputation: 3470

SELECT tableA.username as userA
    tableB.username as userB
FROM tableA
CROSS JOIN table B
WHERE tableA.username='abc'
    AND tableB.username='abcd'
    AND tableB.password='abcd';

CROSS JOIN! :P CARTESIAN PRODUCT!

Upvotes: 1

Raffaello.D.Huke
Raffaello.D.Huke

Reputation: 552

try this:

select t1.userA,t2.userB  
 from
      (SELECT username AS userA FROM WHERE username='abc')t1
 join (SELECT username AS userB FROM WHERE username='abcd' AND password='abcd')t2
 where 1=1

where 1=1 will combine any 2 queries without condition but it's meaningless. Although it match what you asked i don't suggest people use it

Upvotes: 1

Zamrony P. Juhara
Zamrony P. Juhara

Reputation: 5262

I haven't try it but here something you can try:

SELECT 
  (SELECT username AS userA FROM WHERE username='abc' LIMIT 1) AS 'userA',
  (SELECT username AS userB FROM WHERE username='abcd' AND password='abcd' LIMIT 1) AS 'userB';

Upvotes: 1

ffflabs
ffflabs

Reputation: 17481

You can combine tables and fields with the syntax:

SELECT tableA.username as userA
    tableB.username as userB
FROM tableA
JOIN table B
WHERE tableA.username='abc'
    AND tableB.username='abcd'
    AND tableB.password='abcd';

Unless there's some kind of relation between the tables, you'll be getting the cross product times rows, so it might be an NxM result which is just useless.

Upvotes: 1

Related Questions