1

Is it possible to join Two queries of mysql in a query ??

Like:

select * from a + select * from b

So that I can use them in a single php loop.

2
  • What are the structures of these tables? and how they are related? Commented Oct 10, 2012 at 11:21
  • Do values exist for each table for all rows? If so, SELECT a.*, b.* FROM a INNER JOIN a.id=b.a_id Commented Oct 10, 2012 at 11:23

2 Answers 2

3

If they have the same number of columns and the datatypes are the same in each column, then you can use a UNION or UNION ALL:

select * 
from a 
UNION ALL
select * 
from b

If you provide more details about the tables, data, etc, then there might be another way of returning this data.

A UNION will return only the DISTINCT values, while a UNION ALL selects all values.

If this is the route that you need to take, and you still need to identify which table the data came from, then you can always create a column to identify which table the data is from , similar to this:

select *, 'a' TableName
from a 
UNION ALL
select *, 'b' TableName
from b

This allows you to distinguish what table the data came from.

Sign up to request clarification or add additional context in comments.

Comments

0

I think it is easier creating sql "variables" like:

select varA, varb from TableA, tableB;

and you can just play with values in PHP accessing properties.

That way you can take conditions in the query like:

select varA, varb from TableA, tableB where varA.id = varB.foreingId bla bla...

;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.