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;
Related
For example, let us consider this table:
In this image consists of rows of 8 where names like Mike,Glenn,Daryl,Shane and Patricia is included with their respective ages
Id
Name
Age
1
Mike
25
2
Glenn
19
3
Glenn
19
4
Daryl
56
5
Shane
30
6
Shane
30
7
Patricia
16
Now I want to insert the type of query that will show the names without repetitions like This, not like This
EDIT: I entered the data from first picture. The request is to list the names without duplicates, as shown in the second and third picture but I will not convert them to text.
DISTINCT specifies removal of duplicate rows from the result set.
SELECT DISTINCT Name
FROM tablename
see: use DISTINCT in SELECT
You can use GROUP BY to achieve it.
SELECT * FROM your_table
GROUP BY your_table.name
ORDER BY id
With the data you gave, the result from this query will be:
id
name
age
1
Mike
25
2
Glenn
19
4
Deryl
56
5
Shane
30
7
Patricia
16
Given a table like this:
id number anotherNumber
1 1 10
2 1 20
3 2 20
4 2 10
5 3 10
If I run the query:
Select *, GROUP_CONCAT(number)
FROM docs
GROUP BY number
I will get:
id number anotherNumber GROUP_CONCAT(number)
1 1 10 1,1
3 2 20 2,2
5 3 10 3
see sql fiddle
However I want to get:
id number anotherNumber GROUP_CONCAT(number)
1 1 20 1,1
3 2 20 2,2
5 3 10 3
Basically I want the numbers in the anotherNumber column to be sorted in DSEC order - it should always be the highest one.
I know you can put an ORDER BYin the GROUP_CONCAT but this will only affect the concatenated values, not the "merged ones". So is there a simple way?
You could use:
Select MIN(id) AS ID, number, MAX(anotherNumber) AS anotherNumber,
GROUP_CONCAT(number)
FROM docs
GROUP BY number;
SQLFiddle Demo
You should always wrap columns that are not specified in GROUP BY with aggregate function(unless they are functionally dependent on GROUP BY columns and your RDBMS supports ANSI-99 Optional feature T301, Functional dependencies)
The following code is the easy way to get the desired result:
SELECT id, number, MAX(anotherNumber) AS anotherNumber, GROUP_CONCAT(number)
FROM docs
GROUP BY number;
I have also attached my SQLFiddle.
You can check it. Good Luck!
I'm trying to merge different old values in one single column.
I have this table
id code langtype desc duration
232 1104466 1 IT text 10
233 1104466 2 EN text 10
234 1104466 6 other desc 10
235 1104466 1 Other IT text(different row) 10
236 1104466 2 Other EN text(same row of previous) 10
And i would like to obtain a result like this
id code desc duration
232 1104466 “IT” = “IT TEXT”, EN=”EN TEXT”, “ES”=”Other desc” 10
It is possibile is mysql?
Thank you
Yes, it is possible. Something like this should achieve this effect:
select group_concat(concat(langtype, ' = ', desc))
from table
group by code
Here we first form a value to be extracted from each row (concat(langtype, ' = ', desc)) and then concatenate it for each grouped row (group_concat). You can change delimiters, or add quotes where you need - look at GROUP_CONCAT and CONCAT docs.
At first I would like greet all Users and apologize for my english :).
I'm new user on this forum.
I have a question about MySQL queries.
I have table Items with let say 2 columns for example itemsID and ItemsQty.
itemsID ItemsQty
11 2
12 3
13 3
15 5
16 1
I need select itemsID but duplicated as many times as indicated in column ItemsQty.
itemsID ItemsQty
11 2
11 2
12 3
12 3
12 3
13 3
13 3
13 3
15 5
15 5
15 5
15 5
15 5
16 1
I tried that query:
SELECT items.itemsID, items.itemsQty
FROM base.items
LEFT OUTER JOIN
(
SELECT items.itemsQty AS Qty FROM base.items
) AS Numbers ON items.itemsQty <=Numbers.Qty
ORDER BY items.itemsID;
but it doesn't work correctly.
Thanks in advance for help.
SQL answer - Option 1
You need another table called numbers with the numbers 1 up to the maximum for ItemsQuantity
Table: NUMBERS
1
2
3
4
5
......
max number for ItemsQuantity
Then the following SELECT statement will work
SELECT ItemsID, ItemsQty
FROM originaltable
JOIN numbers
ON originaltable.ItemsQty >= numbers.number
ORDER BY ItemsID, number
See this fiddle -> you should always set-up a fiddle like this when you can - it makes everyone's life easier!!!
code answer - option 2
MySQL probably won't do what you want 'cleanly' without a second table (although some clever person might know how)
What is wrong with doing it with script?
Just run a SELECT itemsID, ItemsQty FROM table
Then when looping through the result just do (pseudo code as no language specified)
newArray = array(); // new array
While Rows Returned from database{ //loop all rows returned
loop number of times in column 'ItemsQty'{
newArray -> add 'ItemsID'
}
}//end of while loop
This will give you a new array
0 => 11
1 => 11
2 => 12
3 => 12
4 => 12
5 => 13
etc.
Select DISTINCT items.itemsID, items.itemsQty From base.items left outer join (select items.itemsQty as Qty from base.items) As Numbers On items.itemsQty <=Numbers.Qty
order by items.itemsID;
Use DISTINCT to remove duplicates. Read more here - http://dev.mysql.com/doc/refman/5.0/en/select.html
It seems like I understood what you asked differently than everyone else so I hope I answer you question. What I would basically do is -
create a new table for those changes.
Create a mysql procedure which given a line in the original table add new lines to the new table - http://dev.mysql.com/doc/refman/5.6/en/loop.html
Run this procedure for each line in the original table.
try this to get distinct values from both columns
SELECT DISTINCT itemsID FROM items
UNION
SELECT DISTINCT itemsQty FROM items
I have table like this:
id products
------ ----------
5 1,2,3
6 2,4,5
9 1,4,7
17 4,6,7
18 1,6,8
19 2,3,6
I have to select only that rows, which row's products column contains one of (2,3) values.
In this case query must return:
id products
------ ----------
5 1,2,3
6 2,4,5
19 2,3,6
But I don't understand how to make construction of this query.
Any ideas?
Thanks in advance.
SELECT id,products
FROM yourTable
WHERE FIND_IN_SET('2',products)>0
OR FIND_IN_SET('3',products)>0
sqlFiddle
Would you mind to try this one please?
select * from TABLE_NAME where products regexp "(^|,)[23](,|$)";
Its doing either two or three at the begining, or at end. Or in between the commas.
Never, never, never store multiple values in one column.
Like you see now this will only give you headaches. Normalize your table. Then you can select normally.
Your table should look like this
id product
-- -------
5 1
5 2
5 3
6 2
6 4
6 5
...
With that table structure your select would be
select id
from your_normalized_table
where product in (2,3)
group by id
having count(distinct product) = 2
That query can make use of indexes and is really fast.