I have 2 tables in MySQL DB.
Both the tables have a column as ID which is of type int(10) unsigned.
Table1 has no data and Table2 has the ID as 24.
I am using the below query to get the max ID
select max(ID) from
(
select IFNULL(max(ID),0) as ID from table1
UNION
select IFNULL(max(ID),0) as ID from table2
)temp;
I am expecting the value 24 but it gives 0.
Anything wrong in my query? Please help.
try this,
SELECT IFNULL(MAX(ID), 0) ID
FROM
(
SELECT ID FROM table1
UNION ALL
SELECT ID FROM table2
) a
Related
I have 4 tables with various columns where ID being the common value in all 4 tables. There is a column called EventDate in all 4 tables but with different values or NULL values for the same ID. The new table should have only ID and EventDate.
I would like to compare all the 4 tables based on the EventDate (ie) if two tables have the same date, it should insert the min date into the new table.
Use UNION ALL to combine all and take Minimum date for every ID
SELECT ID, MIN(EventDate) AS MIN_DTE INTO NEW_TABLE FROM (
SELECT ID, EventDate from TABLE1
UNION ALL
SELECT ID, EventDate from TABLE2
UNION ALL
SELECT ID, EventDate from TABLE3
UNION ALL
SELECT ID, EventDate from TABLE4
)A
GROUP BY ID
I want to have results in a table where the data comes from 3 different tables.
For that I have tried to execute this query:
INSERT INTO sometable (id,date)
VALUES
(
(SELECT id FROM table1
UNION
SELECT id FROM table2
UNION
SELECT id FROM table3)
,
(SELECT date FROM table1
UNION
SELECT date FROM table2
UNION
SELECT date FROM table3)
)
The result of this query is an error stating cannot insert multiple rows. Please help me to write this query correctly.
The INSERT ... SELECT syntax is different to the INSERT ... VALUES syntax. Also, you want to select both columns from each table at the same time:
INSERT INTO sometable (id, date)
SELECT id, date FROM table1 UNION
SELECT id, date FROM table2 UNION
SELECT id, date FROM table3
I have two very simple table: t1 and t2 with the following rows:
table t1:
id, name
1 PBN
table t2:
id, name
100 FIBERHOME
Query 1:
SELECT name FROM t1 UNION SELECT name FROM t2 WHERE id IN (1)
Result is: PBN
Query 2:
SELECT name FROM t1 UNION SELECT name FROM t2 WHERE id IN (100)
Result is: PBN, FIBERHOME
But the expected result is: FIBERHOME..! What is the reason?
To expand on #Knep's answer, if you only want one WHERE id IN ():
SELECT name FROM (
SELECT id, name FROM t1
UNION
SELECT id, name FROM t2
) unioned
WHERE id IN (1,100)
Probably not great speed wise, so best to test.
Note also the id needs to be in the sub query to be used in the outer WHERE.
I thought that the WHERE clause is global – #szpal
To answer the question as to why the WHERE isn't used for all queries in the UNION, think about two queries that don't share a column.
On their own:
SELECT id, name FROM x WHERE colA = 123
And:
SELECT id, name FROM y WHERE colB = 456
Then together with (the incorrect) single WHERE clause:
SELECT id, name FROM x
UNION
SELECT id, name FROM y
WHERE colB = 456 -- But table x doesn't have a colB!
Whereas if (correctly) the WHERE clause sits with each query:
SELECT id, name FROM x
WHERE colA = 123 -- I have a colA, still don't have a colB
UNION
SELECT id, name FROM y
WHERE colB = 456 -- I have a colB, still don't have a colA
Everyone's a winner!
UNION sum up the two results.
In the first query, there is no condition so it returns PBN, then it adds the result of the second result FIBERHOME.
Using UNION you could try:
SELECT name FROM t1 WHERE id IN (100) UNION SELECT name FROM t2 WHERE id IN (100)
The where condition in second query will be executed before union.
SELECT name FROM t1
will return
id name
1 PBN
SELECT name FROM t2 WHERE id IN (100)
will return
id name
null null
The union will combine above two results as
SELECT name FROM t1 UNION SELECT name FROM t2 WHERE id IN (100)
id name
1 PBN
You can solve this by
SELECT
name
FROM
(SELECT
*
FROM
interns_test_db.t1 UNION SELECT
*
FROM
interns_test_db.t2) A
WHERE
ID IN (100)
But this may reduce the performance.
I am trying to display all records from table1 even if the catid not existing in table2 (all employee in table2 should have all catid from table1 with 0 days if not exising in table2) with the following sql query but getting an error
Error Code: 1054. Unknown column 'catid' in 'group statement'
select empid,days from table2 union select catid from
table1 group by empid, catid;
table1:
catid
1
2
3
table2:
empid catid days (computed column count(*))
1000 1 8
1000 3 10
expected result:
empid catid days
1000 1 8
1000 2 0 <---catid 2 and days 0 if catid not existing in table2 for empid 1000
1000 3 10
That is not the function of the union statement. Union statement does a set like capability which merging two sets. What you are looking for a is a join with the table 1 where you do a count and group by catid. Your data model to achieve this output itself is grievously wrong ;)
select employeeid, catid, sum(days) from table1, table2 group by employeeid, catid;
You just need a LEFT JOIN:
Select tab2.empid, tab2.catid, ifnull(tab2.days, 0)
from tab2
left join tab1 on tab2.catid = tab1.catid
Please note : While doing a UNION the number and type of the columns present in the first select should be the same as the next Selects.
So you need to first make the select columns in sync first.
can you check this and add empid similarly.
SELECT TABLE1.CATID, IFNULL(TABLE2.DAYS,0) FROM table1 LEFT OUTER JOIN
table2 ON table1.catid = table2.catid
Please use LEFT JOIN with IFNULL.
Select table2.empid, table1.catid, IFNULL(table2.days, 0) from table2
LEFT JOIN table1 ON table2.catid = table1.catid;
Hi I have a query like that
SELECT column1,MAX(column2) AS MAX FROM
table1 GROUP BY column1 ORDER BY MAX
DESC;
and i have a second table which name table2 and has same column2 but different column1 name,
I want to apply this query to union of these table,when i try this
SELECT column1,MAX(column2) AS MAX FROM
((SELECT * FROM table1) union (SELECT
* FROM table2)) GROUP BY column1 ORDER BY MAX DESC;
I got this error "ERROR 1248 (42000): Every derived table must have its own alias"
how can i do that? thanks for help...
The alias comes after the derived table definition,
SELECT column1,MAX(column2) AS MAX FROM
(SELECT * FROM table1 union SELECT * FROM table2) t3
GROUP BY column1 ORDER BY MAX DESC;
The alias is t3