mysql multiple values in 1 row [duplicate] - mysql

This question already has answers here:
Search with comma-separated value mysql
(1 answer)
Is storing a delimited list in a database column really that bad?
(10 answers)
Closed 11 months ago.
I have this table units
id
value
1
value 1
2
value 2
3
value 3
and another table called users
id_units
concat_value
1,2
---
1,3,2
---
what I'm struggling with is how can I show on the concat_value column all the values separated by comma for each of the ids on the id_units field?
I need it to look like this
id_units
concat_value
1,2
value 1,value2
1,3,2
value 1,value3,value2

Related

I want to do a query in mySQL from horizontal to vertical [duplicate]

This question already has answers here:
MySQL transpose columns sum into rows with column names as a value [duplicate]
(1 answer)
MySQL converting columns to rows
(1 answer)
Closed 4 months ago.
I have this table:
ColumnA
ColumnB
ColumnC
ColumnD
1
2
1
3
3
4
3
4
I want a to do a select like this:
Column id
sum
ColumnA_
4
ColumnB_
6
ColumnC_
4
ColumnD_
7
The porpuse of this is to get a column with the comumn id and another with the sum of each column
I've tried to get the sum of the columns but I couldn't get it like vertically
SELECT SUM(ColumnA ) as ColumnA_, SUM(ColumnB ) as ColumnB_, SUM(ColumnC ) as ColumnC_, SUM(ColumnD ) as ColumnD_ FROM table_name
and I get this (not what I want):
ColumnA_
ColumnB_
ColumnC_
ColumnD_
4
6
4
7

Retrieve different data from one column in MySQL [duplicate]

This question already has answers here:
Count boolean field values within a single query
(4 answers)
Closed 4 months ago.
Suppose a table have a column with values like:
column
3
1
1
3
4
1
2
I want to retrieve that how many times there is 1 in column and how many times > 1.
Is there anyway to do this within one query?
I want the data to be fetched like this:
one | greater_than_one
3 | 4
Thanks in advance.
Simple:
select sum(col1=1) as one ,
sum(col1>1) as greater_than_one
from test_tbl;
https://dbfiddle.uk/348f5YOK

Calculate Total Number of Row's Value [duplicate]

This question already has answers here:
mysql count the sum of all rows
(6 answers)
Closed 6 years ago.
I have a SQL table that looks like this:
id | value
-----------------------
1 | 5
2 | 5
3 | 6
I want to count the value of every rows.
In this example the output should be 16.
What query should I use to count the total value of the rows?
You can learn more about COUNT() Function there.
https://www.w3schools.com/sql/sql_func_count.asp

using substring with regex mysql [duplicate]

This question already has answers here:
How to extract two consecutive digits from a text field in MySQL?
(7 answers)
How to extract the nth word and count word occurrences in a MySQL string?
(9 answers)
Closed 6 years ago.
I am trying to get year (yyyy) from text/string in the column "title" but for some reason can't get following query to work:
select substring(title,LOCATE(REGEXP '[(][0-9]{4}[)]',title),4) from videos
title column has following values:
abc abc 2001 abc
xyz (2002) xyz
Any assistance will be appreciated.

Make comma separate values with single column in mysql [duplicate]

This question already has an answer here:
MySQL : Multiple row as comma separated single row
(1 answer)
Closed 9 years ago.
Here is my table information
Id Name
1 aaa
1 bbb
2 ccc
2 ddd
Ids are the repeated one. Now I want the result like
1 aaa,bbb
2 ccc,ddd
How can I do that in Mysql ?
Use GROUP_CONCAT()
select id, group_concat(name) as names
from your_table
group by id