Select the max value from array in mysql query [duplicate] - mysql

This question already has answers here:
Is storing a delimited list in a database column really that bad?
(10 answers)
Closed 3 years ago.
hi i have the following table (battles) :
+-----------+------------------------------------------+
| id | battles |
+-----------+------------------------------------------+
| 1 | 1;2 |
| 2 | 231;2 |
| 3 | 3330;0 |
| 4 | 11;333 |
| 5 | 32;3324 |
| 7 | 2;1 |
| 8 | 333:233 |
+-----------+------------------------------------------+
The table contains the win and loss of each player (win;lose)
how can i select the biggest win (3330;0)

Your last entry must be
| 7 | 333;233
to work properly. i believe it is only a typo
SELECT id,battles, maximal
From battles t inner join
(Select
MAX(SUBSTRING_INDEX(battles,';',1)+0) maximal
FRom battles) t1 on t1.maximal = SUBSTRING_INDEX(t.battles,';',1);
The result is
d battles maximal
3 3330;0 3330
If you had more ids with 3300 then they would also appear in the result

Related

Selecting for each row values from a related table [duplicate]

This question already has answers here:
MySQL pivot table query with dynamic columns
(3 answers)
Closed 9 months ago.
I am rather new to SQL, and I need some help. Suppose I have two tables, Person (with columns PID and Name) and Visit (with columns PID (fk) and Date), where each Person can have multiple Visits.
I would like to select every person (with a condition, omitted here) with all the visit dates on the same row as the person they belong to, like
| PID | Name | Date | Date | Date |
| ----| -------|--------- |----------|----------|
| 1 | Daniel | 25/01/21 | 13/06/21 | |
| 2 | Nicole | 26/01/21 | 18/06/21 | 07/10/21 |
| 3 | Kayla | 02/02/21 | 25/06/21 | |
I've tried
SELECT PersonID, Name (SELECT Date FROM Visit V WHERE V.PersonID = P.PersonID) FROM Person P
which obvisously doesn't work. MySQL says
#1242 - Subquery returned more than 1 row
which I by all means expected! How can I solve this?
This query will give you a bit different result (dates in one column), but it would be easy to parse if you need to:
SELECT person.PID,person.Name,group_concat(visit_date) as dates from person,visit where person.PID = visit.PID group by person.PID
It will be something like that:
+-----+-------+----------------------------------+
| PID | Name | Dates |
+-----+-------+----------------------------------+
| 1 | Marek | 2022-05-15,2022-05-16,2022-05-12 |
| 2 | Magda | 2022-05-16,2022-05-16,2022-05-16 |
+-----+-------+----------------------------------+```

How can I get the biggest value? [duplicate]

This question already has answers here:
MIN/MAX vs ORDER BY and LIMIT
(6 answers)
Closed 5 years ago.
Here is my table:
-- log
+----+---------+------------+
| id | user_id | seen |
+----+---------+------------+
| 1 | 2342 | 1442664886 |
| 2 | 3244 | 1442665851 |
| 3 | 2342 | 1442711823 |
| 4 | 7654 | 1442864219 |
| 5 | 3244 | 1442954080 |
| 6 | 9984 | 1442984716 |
+----+---------+------------+
I want to get the biggest seen time for a specific user as last seen. I can do that by these two queries:
First query:
SELECT seen AS last_seen
FROM log
WHERE user_id = :id
ORDER BY seen DESC
LIMIT 1
Second query:
SELECT MAX(seen) AS last_seen
FROM log
WHERE user_id = :id
Well which one is the standard way? Should I go with which one? Is there any different in the performance?
They are both fine. Both will take advantage of an index on log(user_id, seen).
The first is often preferable because you can pull in the whole row and get information from other columns.

Mysql - join 3 tables with different number of rows into another [duplicate]

This question already has answers here:
How can I do a FULL OUTER JOIN in MySQL?
(15 answers)
Closed 5 years ago.
I have the following tables and I need to join them:
Table A
+----+----------+--------+
| ID | Period | Value |
+----+----------+--------+
| 1 |2009-02-01| 20.3 |
| 2 |2009-03-01| 22.5 |
| 3 |2009-04-01| 17.4 |
| 4 |2009-05-01| 16.5 |
| 5 |2009-06-01| 26.5 |
| 6 |2009-07-01| 35.4 |
+----+----------+--------+
Table B
+----+----------+--------+
| ID | Period | Value |
+----+----------+--------+
| 1 |2009-04-01| 57.1 |
| 2 |2009-05-01| 56.5 |
| 3 |2009-06-01| 59.8 |
| 4 |2009-07-01| 55.4 |
+----+----------+--------+
Table C
+----+----------+--------+
| ID | Period | Value |
+----+----------+--------+
| 1 |2009-03-01| 82.5 |
| 2 |2009-04-01| 87.4 |
| 3 |2009-05-01| 86.7 |
+----+----------+--------+
My output table is already created empty table and looks like this:
Table D
+----+----------+--------+--------+--------+
| ID | Period | ValueA | ValueB | ValueC |
+----+----------+--------+--------+--------+
As table A contains most of the records I want to use it as primary table. The desired result is as follow:
Table D
+----+----------+--------+--------+--------+
| ID | Period | ValueA | ValueB | ValueC |
+----+----------+--------+--------+--------+
| 1 |2009-02-01| 20.3 | NULL | NULL |
| 2 |2009-03-01| 22.5 | NULL | 82.5 |
| 3 |2009-04-01| 17.4 | 57.1 | 87.4 |
| 4 |2009-05-01| 16.5 | 56.5 | 86.7 |
| 5 |2009-06-01| 26.5 | 59.8 | NULL |
| 6 |2009-07-01| 35.4 | 55.4 | NULL |
+----+----------+--------+--------+--------+
I'm very new to MySQL. I looked to similar questions in the forum and tried to figure it out for myself but with no success.
Any help appreciated.
ANSWER
Ok. Few things. First the question isn't duplicate! The thing looks more like a speedy moderator than duplicate question. And No, I didn't find this answer anywhere on this website and for sure not in the suggested from the moderator answer.
Now the interesting part.
With the help of the creative moderator after spending few hours finally I got it work. As I said in the original question table D already exists. This is because the model is created and managed by third party application. In this case Django. This is an important point as otherwise the operation will be different. I also have more than one schema on the server.
I think In this case the best way for this query is by using alias as follow:
insert into my_schema.table_d(period, valueA, valueB, valueC)
select A.Period, A.Value, B.Value, C.Value
from my_schema.table_a A
left join my_schema.table_b B
on A.Period = B.Period
left join my_schema.table_c C
on A.Period = C.Period
You are looking for a left join:
insert into d( . . . )
select . . .
from a left join
b
on a.period = b.period left join
c
on a.period = c.period;
Fill in the columns where the . . . are.

Compare field values in same row SQL [duplicate]

This question already has answers here:
mysql select lowest price from multi select
(4 answers)
Closed 7 years ago.
table structure
id | name | date_usa | date_jpn | date_uk
---+------+----------+----------+--------
1 | a | 01/2015 | 12/2014 | 04/2015
2 | b | 05/2015 | 05/2015 | 05/2015
3 | c | 08/2016 | 09/2017 | 09/2017
I want it to list out:
earliest release dates:
a: 12/2014
b: 05/2015
c: 08/2016
How can I achieve this?
select name, least(date_usa, date_jpn, date_uk)
from your_table

Self join and recursive selection in a table

Assuming a table as below
| ID | NAME | ROLE | MGRID |
---------------------------
| 1 | ONE | 5 | 5 |
| 2 | TWO | 5 | 5 |
| 3 | THREE | 5 | 6 |
| 4 | FOUR | 5 | 6 |
| 5 | FIVE | 15 | 7 |
| 6 | SIX | 25 | 8 |
| 7 | SEVEN | 25 | 7 |
| 8 | EIGHT | 5 | 8 |
How do I get a list of all employees reporting to an employee, including the ones who are in subsequent reporting levels below?
I mean, given emp id 5, I should get [1, 2] and given 7, I should get [1, 2, 5, 7]. How do I get this done?
Will self joins be of help here? Need to brush up my knowledge on joins now.
SELECT id
FROM emp
START WITH id = 7
CONNECT BY NOCYCLE mgrid = PRIOR id
SQLFIDDLE LINK
Here is a SQL statement using Oracle.
select id, name, role, mgrID
from employees
start with id = 7
connect by NoCycle prior id = mgrid;
Please note that the manager for employee 7 is the employee 7 - they are their own manager. This will cause an error - "Connect By loop in user data'. By using the NoCycle keyword you can tell Oracle to detect this and avoid the error.
Does this solve your issue?
I know this isn't exactly what you were asking, but if you are willing to choose a finite number of level's to recurse it isn't too bad to write.
SELECT table_2.id
FROM table LEFT JOIN
(table AS table_1 LEFT JOIN table AS table_2 ON table_1.id = table_2.MgrID)
ON table.id = table_1.MgrID
WHERE (((table.id)=7));
ETC.