A problem with UNION Query Usage
The issue here is suppose if i want to use two queries seperated by UNION, the query is as
$query=(select a.name,a.ag开发者_StackOverflow中文版e,b.country,b.state from a,b where a.aid=b.bid) UNION (select a.name,a.age,c.profession,c.salary from a,c where a.anid=c.cid)
here the result would only show the first query's result , Any way in which i could display the result of 2nd query also down to the result of first query using UNION. Expecting any help on this. Thanks
Are you after
(
select a.name,a.age,b.country,b.state,null as profession,null as salary
from a,b
where a.aid=b.bid
)
UNION
(
select a.name,a.age,null,null,c.profession,c.salary
from a,c
where a.anid=c.cid
)
You will have null in the profession and salary columns from the first query and null in country and state columns in the second query
Try this
$query=(select a.name,a.age,b.country,b.state from a,b where a.aid=b.bid UNION select a.name,a.age,c.profession,c.salary from a,c where a.anid=c.cid)
by the way the fields in both select must be the same datatype
As I understood it a union was to do the same query on two different tables. If you get a result from the first half of the union you will not get the results from the second half.
Basic property of UNION
is
Selected columns listed in corresponding positions of each SELECT statement should have the same data type. (For example, the first column selected by the first statement should have the same type as the first column selected by the other statements.)
If the data types of corresponding SELECT columns do not match, the types and lengths of the columns in the UNION result take into account the values retrieved by all of the SELECT statements. For example, consider the following:
精彩评论