Retrieve different data from one column in MySQL [duplicate] - mysql

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

Related

How to auto insert data into database using the values from another table? [duplicate]

This question already has answers here:
What is a good pratice to store row counts in another table?
(4 answers)
Trigger update other table with count
(1 answer)
Closed 9 months ago.
I have 2 tables.
this is the incomes table
income_id date_income total_amount
1 2022-05-22 14
2 2022-05-22 15
3 2022-05-21 20
4 2022-05-21 25
and this is the total_income table
total_inc_id income_id date_income total total_income_amount
1 1 2022-05-22 2 29
However I want to count rows with the same date from table1 and store inside table 2 like above automatically whenever i add income. How can i achieve this?

mysql multiple values in 1 row [duplicate]

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

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

Does anyone know how to Stuff in groups? [duplicate]

This question already has answers here:
Can I concatenate multiple MySQL rows into one field?
(16 answers)
Closed 9 years ago.
I am trying to use the stuff function in MS SQL to stuff certain info. Here is the example:
Number Value
1 1
2 1
3 1
1 2
2 2
3 2
1 3
2 3
I would like to stuff the column so that only one record will display as following:
Value Number
1 1,2,3
2 1,2,3
3 1,2
Please note that there are a like n-Numbers and n-Values.
You can use GROUP_CONCAT for this. For example:
SELECT `Value`, GROUP_CONCAT(DISTINCT `Number` ORDER BY `Number`)
FROM `yourTable`
GROUP BY `Value`

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