Select two columns from two table with conditions - mysql

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

Related

SQL order and limit on a result of a query

I have a query that gets rows ordered by column a and limited to 100. Then I want to run a query on that result that gets rows ordered by column b and limited by 50.
How can I do this?
Do the first order by/limit in a derived table. Then do the second order by/limit on the derived table's result:
select * from
(
select * from tablename
order by a
limit 100
) dt
order by b
limit 50
You should use select from select statement:
select a, b
from (
select a, b
from table1
order by a
limit 100
)
order by b
limit 50

How to select different rows from 2 tables

I'am trying to get randomly 3 different rows from 2 tables with different fields
SELECT * FROM `models` JOIN banners WHERE `recomended` = '1' ORDER BY RAND() LIMIT 0,3
I want a result like this:
first row - table1.id table1.name table1.points
second row - table2.id table2.group table.2points
third row - table1.id table1.name table1.points
is that possible to do?
Here is one way to get what you want:
select id, name, points
from ((select id, name, points, 1 as which
from table1
order by rand()
limit 1
) union all
(select id, group, points, 2 as which
from table2
order by rand()
limit 1
) union all
(select id, name, points, 3 as which
from table1
order by rand()
limit 1
)
) t
order by which;

How to solve this particular query?

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...

Mysql union help

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

how to find first and last record from mysql table

I have one table I want to find first and last record that satisfy criteria of particular month.
SELECT
(SELECT column FROM table WHERE [condition] ORDER BY column LIMIT 1) as 'first',
(SELECT column FROM table WHERE [condition] ORDER BY column DESC LIMIT 1) as 'last'
This worked for me when I needed to select first and the last date in the event series.
First and last make sense only when you have the output of the query sorted on a field(s).
To get the first record:
select col1 from tab1 order by col1 asc limit 1;
To get the last record:
select col1 from tab1 order by col1 desc limit 1;
How about something like:
select 'first', f1, f2, f3, f4 from tbl
order by f1 asc, f2 asc
limit 1
union all
select 'last', f1, f2, f3, f4 from tbl
order by f1 desc, f2 desc
limit 1
Obviously feel free to add whatever condition you want in a where clause but the basic premise of the order by is to reverse the order in the two select sections.
The limit clause will just get the first row in both cases. That just happens to be the last row of set in the second select due to the fact that you've reversed the ordering.
If there is only one row resulting from your conditions and you don't want it returned twice, use union instead of union all.
select * from table
where id = (select id from tab1 order by col1 asc limit 1) or
id = (select id from tab1 order by col1 desc limit 1);
Try this one: take for example you want to group your table based on group_col and get the first and last value of value_col:
select substring_index(group_concat(value_col), ',',1) as 'first',
substring_index(group_concat(value_col), ',',-1) as 'last'
from table
group by group_col
SELECT * FROM (
SELECT first_name, LENGTH(first_name) FROM Employees
ORDER BY LENGTH(first_name) ASC
FETCH FIRST 1 rows ONLY)
UNION
SELECT * FROM (
SELECT first_name, LENGTH(first_name) FROM Employees
ORDER BY LENGTH(first_name) DESC
FETCH FIRST 1 rows ONLY)
ORDER BY 2 desc;