I initially had a .csv file, and I imported the data into SQL. It comprises of footballers' data, so each footballer has a football club. Hence, when I create a reference table for the club, it becomes like this, since it reads the football club for each player entry.
id club big_club
1 Arsenal 1
2 Arsenal 1
3 Arsenal 1
......
15 Brighton 0
16 Brighton 0
17 Brighton 0
However, I want
id club big_club
1 Arsenal 1
2 Brighton 0
3 Chelsea 1
4 Everton 0
......
and so on. Currently, I'm thinking of 2 options.
1) Load data and filter directly (most preferred)
2) Load data in first, then update table to find the distinct values
I would like assistance in both. Option 2 sounds rather simple but unfortunately, I only know how to do it from a SELECT DISTINCT standpoint and not an UPDATE standpoint.
For loading data into the table, this is what I have.
LOAD DATA INFILE 'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/epldata_final.csv'
INTO TABLE big_clubs
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(#name, #club, #age, #position, #position_cat, #market_value, #page_views, #fpl_value,
#fpl_sel, #fpl_points, #region,
#nationality, #new_foreign, #age_cat, #club_id, #big_club, #new_signing)
SET id=#id, club_id = #club_id, club = #club;
I tried SET club_id = DISTINCT #club_id but that doesn't work.
Would appreciate help / guidance for both methods.
I have parsing Queries with below references
link1 - SET and Select Query combine Run in a Single MySql Query to pass result in pentaho
link2
Input will be shown in below Col1 showing ,In #input in the above reference link i am considering only 1 records and applying parsing logic for each cell , but issue is with multiple rows (n rows) and combining result with parsing logic.
Col1
--------------
22:4,33:4
33:6,89:7,69:2,63:2
78:6
blank record
22:6,63:1
I want to create single Query for same as in reference link i asked for.
Expected Output
xyz count
------------
22 10
33 10
89 7
69 2
63 3
78 6
I tried solutions Passing values with this conditions
where condition pass 1 by 1 col1 in (my query)
MAX (col1)
group_concat
but i am not getting expected output to fit this all things in a single query.
I finally found solution for my question. and group_concat worked for this
#input= (select group_concat(Col1) from (select Col1 from table limit 10)s);
group_concat will merge all the rows of Col1 into comma seperated string
22:4,33:4,33:6,89:7,69:2,63:2,78:6,blank record,22:6,63:1
as we have now single string we can apply same logic as shown in link 1
we can replace blank record with REPLACE command and neglect it.
Output after using logic from link1 result
xyz count
------------
22 4
33 4
33 6
89 7
69 2
63 2
78 6
22 6
63 1
Just use Group by
select xyz,sum(count) from (select link1 output)s group by xyz;
will give you Final Output
xyz count
------------
22 10
33 10
89 7
69 2
63 3
78 6
There are few rows:
Id marks
1 15
1 16
1 17
2 6
2 15
3 9
3 10
I want to merge all the id into a single row, with
output like below:
Id marks1 marks2 marks3
1 15 16 17
2 6 15
3 9 10
Concatenating fields into a single field is made really easy in mysql. there is function for it. CONCAT(). Better yet if you and want to delimit them by the same string you can use CONCAT_WS(). which is concatenate with separator, the first parameter being the separator and any other argument after being a field you wish to join.
http://www.w3resource.com/mysql/string-functions/mysql-concat_ws-function.php
SELECT id, CONCAT_WS(' ', marks1, marks2, marks3) as 'mark1 | mark2 | mark3' from my_table group by id;
UPDATE
I think I Misunderstood your schema structure. I think what you actually are looking for is GROUP_CONCAT().
http://www.w3resource.com/mysql/aggregate-functions-and-grouping/aggregate-functions-and-grouping-group_concat.php
SELECT id, GROUP_CONCAT(marks) as 'All Marks' from my_table group by id;
I need to store a set of integers in MYSQL. The problem is as : I have an id(integer). Each id is mapped to a set of numbers. The numbers in the set can have values from 1 - 160. So my table should be of structure {id,set}. Also each id'set can have different length (max 40 numbers).
Eg:
id set
1 {2 45 46 67 89 155}
2 {1, 11 12 34 56 67 68 79 80 134 145}
I have tried the SET datatype in MYSQL. I defined it as
CREATE TABLE mytable ( id NUMBER PRIMARY KEY , mycol SET('1','2','3','4'...))
But the problem is the set allows to define only 64 elemnets.
Note: I need options other than creating a mapping table as :
id setno
1 2
1 45
....... and so on
Can anyone suggest another way to store a set of numbers in MYSQL.
Create child table to hold the numbers, one per row:
create table set_number (
set_id int,
num int
);
Then insert your numbers. To get them as one CSV value, use group_concat(), something like:
select t.id, group_concat(s.num) set
from mytable t
join set_nimber s
on t.set_id = s.set_id
group by t.id
The table structure you described is the right way to do.
And its all in the way you present data, create a table where it maps ID into Sento
And ID is a primary key
So that you will have:
ID Sento
1 2
1 45
1 46
...
1 155
And then have an SQL that says:
SELECT ID, GROUP_CONCAT(Sento, SEPARATOR ', ')
FROM Table
GROUP BY ID
Hope this helps
I have a table named 'table1' with the columns:
CSMembers,BCID,Total,Email.
The excel sheet contains the data for this table in this format:
CSMembers BCID Total Email
abc 2,5,7,9,12,17,22,32 10,000 abc#gmail.com
xyz 1,3,5,7,9,12,17,20,22,33 12,500 xyz#gmail.com
pqr 2,5,7,9,12,17,22,32 11,000 pqr#gmail.com
ttt 2,5,7,9,12,17,22 9,800 ttt#gmail.com
the .csv file of this is :
CSMembers,BCID,Total,Email
abc,"2,5,7,9,12,17,22,32","10,000",abc#gmail.com
xyz,"1,3,5,7,9,12,17,20,22,33","12,500",xyz#gmail.com
pqr,"2,5,7,9,12,17,22,32","11,000",pqr#gmail.com
ttt,"2,5,7,9,12,17,22","9,800",ttt#gmail.com
I have used the following code:
load data local infile 'H:/abc.csv' into table table1
fields terminated by ','
optionally enclosed by '"'
lines terminated by '\n' ignore 1 lines
(CSMembers,BCID,Total,Email);
I am getting the following output:
CSMember BCID Total Email
abc 2 10 abc#gmail.com
xyz 1 12 xyz#gmail.com
pqr 2 11 pqr#gmail.com
ttt 2 9 ttt#gmail.com
But i need this output:
CSMembers BCID Total Email
abc 2,5,7,9,12,17,22,32 10,000 abc#gmail.com
xyz 1,3,5,7,9,12,17,20,22,33 12,500 xyz#gmail.com
pqr 2,5,7,9,12,17,22,32 11,000 pqr#gmail.com
ttt 2,5,7,9,12,17,22 9,800 ttt#gmail.com
Can anyone please tell me what is wrong?
if I should change the code or the csv file content or both?
plese help.
Your schema for the table is possibly wrong bcid and total is probably defined as an int of some kind instead of a string. Numeric fields won't know whether a comma is a separator to another field or simply dividing the number up so it is easily readable. Also input to numeric fields typically accepts the value up to the first non-numeric character i.e. the comma