how to order a mysql selection - mysql

I'm selecting data from two different tables which have a common field "date" and i want to order by date desc but i can't get it to work and i haven't found much help googleing about it.
SELECT table1.id, table1.date, table2.id, table2.date FROM table1, table2 ORDER BY
Also i would appreciate any links to read more about these types of queries, it would save me from having to ask here for help all the time :D Thanks.

Unless you want a full-blown cartesian product (where you combine every row in table 1 with every row in table 2), you'll need a limiting where clause, something like:
select a.id, b.id, b.date
from table1 a, table2 b
where a.date = b.date
order by b.date desc
The where clause in the above select will only combine rows from the two tables where the dates are identical, which is what it sounds like you need.
If instead, you want to get the ID and date from two different tables and sort by the date, you probably need something like:
select id, date
from table1
union all
select id, date
from table2
order by 2 desc
This will return the IDs and dates from both tables, unioned together, then sort using a column number rather than name.

Something like:
SELECT table1.id, table1.date, table2.id, table2.date FROM table1, table2 ORDER BY table1.date
An introduction to SQL ORDER BY could be found here: http://www.w3schools.com/sql/sql_orderby.asp.

SELECT table1.id, table1.date, table2.id, table2.date FROM table1, table2 ORDER BY table2.date DESC
if you want to order by table2's date
OR
SELECT table1.id, table1.date, table2.id, table2.date FROM table1, table2 ORDER BY table1.date DESC

You could use UNION ALL to combine the table queries, something like this:
SELECT * FROM
(
SELECT 1 AS id, date FROM table1
UNION ALL
SELECT 2 AS id, date FROM table2
) T1
ORDER BY date

Related

Select Distinct between two tables inner join

I have table 1 which contains unique values and table 2 which contains multiple values for the same email. What I want to do is select the first value of 'id' - table 2 contains a number of ids and matching emails
SELECT DISTINCT
table1.email,
table2.id FROM
table1
INNER JOIN users ON table1.email = table2.Email
the problem is the output needs to be unique - i.e. one ID - the first one from table2 that is associated with a given email - currently we're getting multiple results - no unique or distinct values.
Probably
any id - add ORDER BY RAND()
1 row of result - add LIMIT 1.
So, the query might be something like this:
SELECT DISTINCT
table1.email,
table2.id
FROM table1
INNER JOIN table2 ON table1.email = table2.email
ORDER BY RAND()
LIMIT 1
Based on new information, it seems like you're looking for something like this instead:
Note: Works on MySQL v8+ and MariaDB 10.2+ that have window function:
SELECT email, id
FROM
(SELECT table1.email,
table2.id,
ROW_NUMBER()
OVER
(PARTITION BY table1.email ORDER BY table2.id) AS 'RowNumber'
FROM table1
INNER JOIN table2 ON table1.email = table2.email) t
WHERE RowNumber=1;
Assign ROW_NUMBER() with table1.email as partition and sort by table2.id ascending (note that the default sorting of ORDER BY is ascending so there's no need to define it as ORDER BY xxx ASC).
Turn the base query into a subquery then do a SELECT .. with condition of WHERE RowNumber=1. Hence, it will return only a single row for each email.
Alternatively, depending on your data, you could just simply do something like this:
SELECT table1.email,
MIN(table2.id) AS minID
FROM table1
INNER JOIN table2 ON table1.email = table2.email
GROUP BY table1.email;
Demo fiddle

How to SELECT date and time within that date?

I read few articles about this: Select max date, then max time This one seems most helpful but I do not see way to implement it.
There is five tables. I join them. I need to select only one row with highest date and highest time from first table and same from second table and join the rest on some other value. With the code I wrote I get multiple rows. It seems time selection is not right.
It might be done with subquery in subquery. I've tried something like this:
SELECT * from table1
INNER JOIN table2 ON table1.date = table2.date AND table1.gm = table2.gm
INNER JOIN table3 ON table2.gm = table3.gm ...
WHERE table3.date = :date AND table4.date = :date ...
AND table1.date IN(
SELECT MAX(table1.date) FROM table1 WHERE table1.time IN(
SELECT MAX(table1.time) FROM table1
)
)
AND table2.date IN(
SELECT MAX(table2.date) FROM table1 WHERE table2.time IN(
SELECT MAX(table2.time) FROM table2 )
)
ORDER BY table1.id
Question is:
How to get single row after joining all of this where date is highest and time is highest on that date?
Thanks!
EDIT: I am sorry for this. I forgot to say that I need max time of max date related with specific value from tables(gm columns). So that is one row(in example I gave it is table1.gm and table2.gm ... ) for each one of that .gm values which are same in every table, not just one row all together. Solutions Nick and Salim provided works but I did not solved problem.
EDIT: SOLVED! after implementing solutions by Nick I just neded to add GROUP BY cntrs_reper.gm_company_no, cntrs_reper.date.
And that's it. For every row in one table enties with highest date and time from others!! Thanks to all.
EDIT. If this can help this is full query:
SELECT cntrs_gm.gm_company_no AS company_c_g,
bns_gms.ded_bns AS ded_bns_gms,
bns_gms.no_ded_bns AS no_ded_bns_gms,
bns_gms.wag_ded_bns AS wag_ded_bns_gms,
cntrs_gm.cur_credit AS cur_credit_c_g,
cntrs_gm.cdrop AS cdrop_c_g,
cntrs_gm.total_jp AS total_jp_c_g,
cntrs_gm.games AS games_c_g,
cntrs_gm.wgames AS wgames_c_g,
cntrs_gm.doors AS doors_c_g,
cntrs_gm.power AS power_c_g,
cntrs_gm.total_in AS total_in_c_g,
cntrs_gm.total_out AS total_out_c_g,
cntrs_gm.total_acc AS total_acc_c_g,
cntrs_gm.total_bet AS total_bet_c_g,
cntrs_gm.total_win AS total_win_c_g,
cntrs_gm.total_bonus AS total_bonus_c_g,
cntrs_gm.date AS date_c_g,
cntrs_reper.gm_company_no AS company_reper,
bns_reper.ded_bns AS ded_bns_reper,
bns_reper.no_ded_bns AS no_ded_bns_reper,
bns_reper.wag_ded_bns AS wag_ded_bns_reper,
cntrs_reper.cur_credit AS cur_credit_reper,
cntrs_reper.cdrop AS cdrop_reper,
cntrs_reper.total_jp AS total_jp_reper,
cntrs_reper.games AS games_reper,
cntrs_reper.wgames AS wgames_reper,
cntrs_reper.doors AS doors_reper,
cntrs_reper.power AS power_reper,
cntrs_reper.total_in AS total_in_reper,
cntrs_reper.total_out AS total_out_reper,
cntrs_reper.total_acc AS total_acc_reper,
cntrs_reper.total_bet AS total_bet_reper,
cntrs_reper.total_win AS total_win_reper,
cntrs_reper.total_bonus AS total_bonus_reper,
cntrs_reper.date AS date_reper,
cntrs_reper.time AS time_reper,
bns_reper.time AS time_c_g,
gms_cfg.gm_no AS machine_id,
gms_cfg.denom_cin AS machine_cin
FROM bns_gms
INNER JOIN cntrs_gm
ON bns_gms.gm_company_no = cntrs_gm.gm_company_no AND bns_gms.date = cntrs_gm.date
INNER JOIN bns_reper
ON cntrs_gm.gm_company_no = bns_reper.gm_company_no
INNER JOIN cntrs_reper
ON bns_reper.gm_company_no = cntrs_reper.gm_company_no AND bns_reper.date = cntrs_reper.date
INNER JOIN gms_cfg
ON cntrs_reper.gm_company_no = gms_cfg.gm_no
WHERE bns_reper.date IN(
SELECT MAX(DATE(bns_reper.date)) FROM bns_reper WHERE bns_reper.time IN(
SELECT MAX(TIME(bns_reper.time)) FROM bns_reper
)
)
AND cntrs_reper.date IN(
SELECT MAX(DATE(cntrs_reper.date)) FROM cntrs_reper WHERE cntrs_reper.time IN(
SELECT MAX(TIME(cntrs_reper.time)) FROM cntrs_reper
)
)
ORDER BY cntrs_gm.gm_company_no
DB example
bns_gms
bns_reper
cntrs_gm
cntrs_reper
gms_cfg
The problem with your current query is that it will select all rows where table1.date is the latest date on which the highest time occurs, which may well be more than one e.g. for data such as
id date time
1 2018-03-30 18:40
2 2018-03-31 12:20
3 2018-03-31 19:20
Your WHERE clause:
table1.date IN(
SELECT MAX(table1.date) FROM table1 WHERE table1.time IN(
SELECT MAX(table1.time) FROM table1
)
will select rows with id=2 and id=3 as they both have date = '2018-03-31' which is when the maximum time occurs.
What you want to do is select the row which has the latest time on the latest date, for which you could use
table1.date = (SELECT MAX(date) FROM table1) AND
table1.time = (SELECT MAX(time) FROM table1 WHERE date = (SELECT(MAX(date) FROM table1))
By using aliasing, that can be simplified (since we already know table1.date = MAX(date) FROM table1) to
table1.date = (SELECT MAX(date) FROM table1) AND
table1.time = (SELECT MAX(time) FROM table1 AS t1 WHERE t1.date = table1.date)
I don't have MySQL but here is the general idea you can use. I don't have enough points to write a comment so I am responding as a reply. Essentially make a subquery/inline view for each table to select max of a column, then join those subqueries/inline views together.
Here is Oracle syntax. You can convert it to ANSI syntax.
select table1.column1, table2.column2,table3.column3
from
(select id1, max(column1) as column1 from table1 group by id1) as table1
(select id2, max(column2) as column2 from table2 group by id2) as table2
(select id3, max(column3) as column3 from table3 group by id3) as table3
where
table1.id1 = table2.id2
and table1.id1 = table3.id3
;

SQL - Order By Sum of Multiple Tables Sharing Common Field

I have 2 different tables sharing the same column names. The tables list the same products which are identified by 'id'. The products have different revenues throughout both tables and are listed multiple times in each table.
I would like to sum the revenue of the same products across the 2 tables and ORDER BY the sum. Result is sorting the highest revenue products first.
I've tried JOIN and UNION but can't seem to figure out the right solution.
UNION query I tried...
SELECT id, SUM(rev) as total
FROM (
SELECT id, rev FROM table1 UNION ALL
SELECT id, rev FROM table2 UNION ALL
)
ORDER BY total DESC
JOIN query I tried...
SELECT table1.id,
table1.rev,
table2.id,
table2.rev,
(table1.rev + table2.rev) as revenue
FROM table1
INNER JOIN table2 ON table1.id = table2.id
ORDER BY revenue DESC
You were close. You needed:
one UNION ALL, not two.
a GROUP BY, that gives the break field.
An alias for the subquery (I used AllRevenue - you can use any valid name.)
SELECT id, SUM(rev) as total
FROM (
SELECT id, rev FROM table1 UNION ALL
SELECT id, rev FROM table2
) AS AllRevenue
GROUP BY id
ORDER BY total DESC
The join approach would have worked if you used a FULL OUTER JOIN, because some ids may be present in one table but not the other, but that is usually less performant.
Looks like you just need to group on the ID then... unless I'm missing something.
select d.* from (
SELECT
table1.id,
Sum(table1.rev) + Sum(table2.rev) as revenue
FROM table1
INNER JOIN table2 ON table1.id = table2.id
GROUP BY
table1.id) d
order by d.revenue
SELECT id, SUM(rev) as total
FROM (
SELECT id, rev FROM table1 UNION ALL
SELECT id, rev FROM table2
)
GROUP BY id
ORDER BY total DESC

Why does group by and sort by give 0 response

The result of this query gives me 0 rows where it should give me the 3 latest rows (grouped by Table1.Name).
Table1 has: "Name", "Timestamp", "Voltage".
Table2 has: "Name", "data".
When I delete "ORDER BY Table1.Timestamp" I do get 3 rows (as expected) but they are the 3 oldest entries in the database where I want the 3 latest.
(I have 3 Name values in Table1 and Table2 that match).
The code:
SELECT * from Table1
INNER JOIN Table2
ON Table1.Name=Table2.Name
GROUP BY Table1.Name
ORDER BY Table1.Timestamp;
You can try to perform a query like this :
SELECT t.*,t2.* from Table1 t
INNER JOIN Table2 t2
ON t.Name=t2.Name
WHERE t.Timestamp = (
SELECT MAX(t3.Timestamp) FROM Table1 t3
WHERE t3.Name = t.Name
)
You could sort and taking the top 3:
SELECT * from Table1
INNER JOIN Table2
ON Table1.Name=Table2.Name
ORDER BY Table1.Timestamp DESC
LIMIT 3
When you are using join two or more tables then don't use * in select query. Use specific column name to avoid column ambiguously in select query , you can also use Table1., Table2. but good way to use specific columns in select query.
SELECT Table1.Name,Table1.Timestamp from Table1
INNER JOIN Table2
ON Table1.Name=Table2.Name
GROUP BY Table1.Name
ORDER BY Table1.Timestamp;

SQL Query with Typo not working in MySQL (Order by on calculated field)

I have this query in which one of the column is a calculated one. Every thing is working except it is not ordering the results when I use that calculated column in query. The query is a very large one so I have simplified it below for understanding. Here the calculated column is "remaining"
SELECT t1.id, t1.name, t2.duration - datediff(now(), t1.posting_time) as "remaining"
FROM table1 t1, table2 t2
WHERE td.id = t1.timefield
ORDER BY id, name, remaining DESC
Even if I remove this "remaining" from order by clause or use it with asc or desc, nothing happens and order stays the same.
The only time when 'remaining' alters the sort order is when the id and the name in two rows are the same. Otherwise, it has no effect on the ordering.
You need to fix the typo of 'td.id' to 't2.id'.
You should learn the JOIN notation too:
SELECT t1.id, t1.name, t2.duration - datediff(now(), t1.posting_time) as "remaining"
FROM table1 t1
JOIN table2 t2 ON t2.id = t1.timefield
ORDER BY id, name, remaining DESC
use a trick:
SELECT *
FROM (
SELECT
t1.id,
t1.name,
t2.duration - datediff(now() , t1.posting_time) as "remaining"
FROM table1 t1, table2 t2
WHERE t2.id = t1.timefield
) AS i
ORDER BY id, name, remaining DESC