Compare field values in same row SQL [duplicate] - mysql

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

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 |
+-----+-------+----------------------------------+```

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

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

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 - Create new rows from existing rows with comma separated string column [duplicate]

This question already has answers here:
Turning a Comma Separated string into individual rows
(16 answers)
SQL split values to multiple rows
(12 answers)
Closed 5 years ago.
I have the following table (named table A):
id | col A (varchar)| col B (varchar)
1 | foo | 1,2,3,...
2 | bar | 4,5,6,...
... | ... | ...
I want to transform it to the following in the same table A:
id | col A (varchar)| col B (int)
3 | foo | 1
4 | foo | 2
5 | foo | 3
6 | foo | ...
7 | bar | 4
8 | bar | 5
9 | bar | 6
10 | bar | ...
... | ... | ...
Do I have sql script way to do that?

Database structure for multiple values [duplicate]

This question already has answers here:
have address columns in each table or an address table that is referenced by the other tables?
(13 answers)
Closed 6 years ago.
I have the following database structure:
categories:
cat_id | name
-------+--------
1 | test 1
2 | test 2
date:
id | text | cat_id
---+--------+--------
1 | google | 1
2 | fb | 2
3 | yahoo | 1,2
I want to display "yahoo" in both categories. I know it's not good to store two values in the same cell. My last idea is to insert two rows with same text but different cat_id.
Is there any other way to do this?
You can have it like this:
categories:
cat_id | name
-------+--------
1 | test 1
2 | test 2
date:
id | text | cat_id
---+--------+--------
1 | google | 1
2 | fb | 2
3 | yahoo | 1
4 | yahoo | 2