Reputation: 75
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
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
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
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
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