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
Related
I have 2 tables t1 and t2 as follows
I want to select the results from both the tables with a UNION and sort on qty column. The query that I have written is
(SELECT * FROM t1)
UNION ALL
(SELECT * FROM t2)
ORDER BY qty ASC;
That gives me the below result
Can some 1 tell me what is going wrong here? As per the query I should get qty column sorted in ascending order. I have also tried
(SELECT * FROM t1
UNION ALL
SELECT * FROM t2)
ORDER BY qty ASC;
No luck with that either. If I do sort with id or item it works perfectly fine. This is really frustrating. Please help.
Possible duplicate of Sorting varchar field numerically in MySQL
Try
(SELECT * FROM t1
UNION ALL
SELECT * FROM t2)
ORDER BY CAST(qty as SIGNED INTEGER) ASC;
I want to
SELECT table1.columnA FROM table1 + ORDER BY id DESC LIMIT 1; //to get last row
and
SELECT SUM(table2.columnB) FROM table2
and then i want to do a minus condition like
SELECT table1.columnA FROM table1 ORDER BY id DESC LIMIT 1 - SELECT SUM(table2.columnB) FROM table2
but it does not work.
If what you want is the result between subtracting the sum of columnB from columnA so use this:
SELECT table1.id,table1.columnA -(select sum(columnB) from table2) as YourColumn
FROM Table1
ORDER BY table1.id desc LIMIT 1
I have a mysql table with following columns:
Id1, id2, timestamp
The id2 is an auto increment entry. The id2 is not unique. so you may have a following rows:
1,12, 983475
2,12, 092348
3,23, 987455
4,23, 908457
I need to get following rows where the timestamp is the latest on the id2.
ie the results will be:
1,12,983475
3,23,987455
Also, the numbers 987455 and 983475 are just fictitious...
Help please...
You can use a subquery with the max() aggregate for this:
select y.id1, y.id2, y.timestamp
from yourtable y
join (select id2, max(timestamp) maxtimestamp
from yourtable
group by id2
) y2 on y.id2 = y2.id2 and y.timestamp = y2.maxtimestamp
This could possibly return ties -- if you truly want a single row per distinct id2, then you can use user-defined variables to establish a row number.
SELECT id2 FROM table ORDER BY max(timestamp) DESC LIMIT 0, 1
Select and restrict using max(timestamp)
e.g. select * from table where timestamp=(select max(timestamp) from table
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
SELECT id, count(*) as Number
FROM (SELECT id FROM t1
UNION ALL
SELECT id FROM t2
UNION ALL
SELECT id FROM t3
) t
GROUP BY id
ORDER BY Number DESC
This is the query giving me the correct result. But When I want to add where call it is throwing error.
SELECT id, count(*) as Number
FROM (SELECT id from t1
UNION ALL
select id from t2
UNION ALL
select id from t3
) t
WHERE Number > 10
GROUP BY id
ORDER BY Number DESC
You want to test conditions on an aggregate function with a HAVING clause rather than a WHERE.
select id, count(*) as Number
from (select id
from t1
UNION ALL
select id
from t2
UNION ALL
select id
from t3) t
group by id
having Number > 10
order by Number desc;
select
id,
count(*) as Number
from
(
select
id
from
t1
UNION ALL
select
id
from
t2
UNION ALL
select
id
from
t3
)t
group by
id
HAVING
Number > 10
order by
Number desc;
Try that - I think its easier to debug if you make your query easy to read...