Consolidating data from multiple fields to a single field [duplicate] - mysql

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Concatenate many rows into a single text string?
I have 2 tables with 1 to many relationship between them.
I need to consolidate data contained in 1 column in the 2nd table and add it to corresponding single entry in the first table.
How could I do that in MySQL ?
ID is the primary key
Example:
Table 2 Calculation
ID Forms Calc
1 A 20
1 B 30
1 C 10
Target Table :
Table1 Client
ID Name Forms
1 XYZ A,B,C

INSERT INTO Client (ID, Forms)
SELECT ID , GROUP_CONCAT(Forms)
FROM Calculation
GROUP BY ID

Related

Remove duplicated based on two columns [duplicate]

This question already has answers here:
How to select and/or delete all but one row of each set of duplicates in a table?
(2 answers)
How can I remove duplicate rows?
(43 answers)
Closed 1 year ago.
I've a flights table that consists of few columns but somebody seem to have ran a migration twice that resulted in creation of same data twice.
Anyway, the flight should only have only data from the following condition: The flight_number and the date.
Basically the table is looking like this at the moment:
flight_number
date
123
2021-09-16
123
2021-09-16
123
2021-09-17
124
2021-09-18
124
2021-09-18
Result I want:
flight_number
date
123
2021-09-16
123
2021-09-17
124
2021-09-18
Basically, keep only one and remove duplicated (if the flight_number is same of the same date).
I'm looking for a DELETE SQL query but couldn't find the one like I am looking for.
What is the query that can help me achieve it?
Thanks!
EDIT: Yes, all the data has a column id that is unique even if the data is same.
You need to identify which rows to keep and which to remove; this can be done as such:
delete ff from
flight ff
inner join (
select flight_number, row_number() over (partition by flight_number order by date) as RN
from flight f
) dups
on ff.flight_number = dups.flight_number
where dups.rn > 1
Basically, this uses Row_Number to create a row identifier based on certain criteria, in this case, for each (partition) Flight_number, create a row number then delete any records where the row_number is > 1.
You will need to change this to use the actual ID column on the join, like this https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=58a4ac7235ea22b557116ad68c8449c3

Select Min or Max record from a table along with the corresponding name field [duplicate]

This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 2 years ago.
Assuming a table Family with Name and Age and records
Bob 55
Alice 40
Marky 12
If I run
Select Name,Min(Age) from Family
I get
Bob,12
I'm trying to ask for the fields from the single record with the lowest age yet I'm simply getting the first record Name and the Age from the record with the lowest value.
How can I use Min() to make that request?
I understand that you want the entire row that has the smallest age. Assuming that you don't care about ties, you can just sort the rows by ascending age and retain the first record only:
select *
from family
order by age
limit 1

Can I retrieve 2 different results from 1 table in a single query? [duplicate]

This question already has answers here:
Find total number of results in mySQL query with offset+limit
(7 answers)
Closed 6 years ago.
I am making a pagination function, here is my case:
1 table (example)
id | title | date | details
Now, I want to retrieve two different results from this table (example)
Count all of the rows (for the total count of all the lists)
I will only show every 10 list per page.
My current code is, I have 2 separated queries for 1 and 2, so it is like 2 connections, my question is, can this be done with a single query and then retrieve both of 1 and 2 results? If so, what do I need to do? Any suggestion/s can help me!
I think,
This will help you.
Step 1: Get the all list from the table
Step 2: Then count the records
Here is the single query to perform it.
SELECT COUNT(tmp.id) as cnt, tmp.* FROM (SELECT id, title, date, details FROM tablename) tmp

Insert date from two tables in MySql

Consider the following two table
Table A:
id int auto_increment
with 2 rows of data.
id
1
2
Table B:
id auto_increment
aid reference to A.id
with 3 rows of data
id aid
1 1
2 2
3 2
If now the table A has been inserted 2 rows to
Table A:
id
1
2
3
4
So, how to write the insert statement so that it can insert to table B as result
Table B:
id aid
1 1
2 2
3 2
4 3
5 4
6 4
7 5
8 6
9 6
The question is for study only. I know there are many ways to do it, but I am just wondering if it can be done by using sql or just in mysql?
UPDATE
Sorry, the question since to be unclear. Let me state it in clear.
The data in table B has relation to table A. B.aid = A.id
The new data in table A, which is A.id, are in sequence, also has relation to the first two id. That means 1 and 3 with the same meaning, 2 and 4 also.
In the insertion of table B, it should consider both 1. and 2. That means, since with one aid=1 and two aid=2, then the data that needs to insert into table B is one aid=3, two aid=4, one aid=5 and two aid=6.
The question: This can be done easily with programming, however, I just wondering can 3. be done in a insert statement with out programming in Mysql?
Assuming you are inserting the ID(s) into Table A,
INSERT INTO [Table B](aid) VALUES ([THE ID])
Where this will insert the specified ID, in the aid column in Table B.
Otherwise if you want to match up the values you can use NOT IN to detect values that are not like, then you must insert these values,
This will select all IDs that do not exist in Table B:
SELECT id
FROM [Table A]
WHERE [Table A].id Not In (SELECT aid FROM [Table B])
...then later (depending what you're using it in, PHP? Then fletch the data (should look something like this))...
while($row = mysqli_fetch_assoc(...))
{
INSERT INTO [Table B](aid) VALUES($row['aid'])
}

SQL to return list of fields containing No data [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
SQL: Select columns with NULL values only
Find fields that aren't used (have all nulls)
I have a table with 400 feilds and 3 Bil rows out of which 100 of them have no values/data. Can some one please give me a sample example or suggestion on how to proceed to get the list of those 100 columns which has null for all the 3 Bil rows in that table.
Thank you in advance.