This question already has answers here:
Sum Two Columns in Two Mysql Tables
(2 answers)
Closed 10 years ago.
I have situation like this:
- name value
-------------
- stuff_1 2
- stuff2 5
- stuff2 3
- stuff_1 4
Which mysql query do I have to use in order to sum all these values and to get something like this:
- name value
-------------
- stuff_1 2+4=6
- stuff2 5+3=8
You need something like this:
select name, sum(value) from table_name group by name
Take a look at MySQL GROUP BY and aggregation functions listed there
Here you go:
SELECT name, SUM(value) FROM your_table GROUP BY name;
Related
This question already has answers here:
mySql copy rows into same table with key value changed (not overwriting existing)
(3 answers)
Closed 4 years ago.
I have one table with the next content
stat_id|stat_type|stat_value
1 |likes |100
1 |reposts |150
Is it possible to clone these rows for another stat_id's in one query?
It's important that we want copies for several new stat_id.
Use INSERT ... SELECT
insert into your_table (c1, c2, ...)
select c1, c2, ...
from your_table
where id = 1
This question already has answers here:
How can I SELECT rows with MAX(Column value), PARTITION by another column in MYSQL?
(22 answers)
Closed 5 years ago.
I have a table with column id and text as below:
id text
001 hello
002 hello
003 hi
004 hello
005 hi
006 test
I need to show list of suggestion for given id, say '001'
Now its going to fetch all the possible records. Even if I apply DISTINCT here I doubt it will still show all values as their ids are unique.
Is it possible to select one value only for 'hello'? If yes, which Id will it show? I think its not a good idea to select this way or is it a common case?
What I'm expecting is, the suggestion list should be as below:
id text
001 hello
003 hi
006 test
Unfortunately, I couldn't use GROUP BY here as I'm using LIKE in the query.
SELECT id, text FROM `tablename` AS `table` WHERE `id` LIKE '00'
Please advise.
referred here: https://dev.mysql.com/doc/refman/5.7/en/exists-and-not-exists-subqueries.html
This solution works for my case:
SELECT DISTINCT text FROM tablename
WHERE EXISTS (SELECT * FROM tablename
WHERE `id` LIKE '%00%')
This question already has answers here:
Query with multiple values in a column
(4 answers)
MySQL search in comma list [duplicate]
(5 answers)
Closed 7 years ago.
I'm wanting to create a sql select statement that will grab rows if a given value is in a comma separated list in one of the columns of the database table.
Example Table...
id | courses
1 | 5, 8, 15, 19
I want to do something like this
$course_num = 5;
$sql = "SELECT * FROM courses WHERE $course_num IS IN courses";
1.) I don't think the "IS IN courses" part is legit. How can I do this?
2.) For my code above, I would want to return the row because of the "5" in courses, not because of the "15". So, if $course_num = 9 no rows should be returned.
Thanks for your help.
By adding comma in searched occurrence
SELECT *
FROM courses
WHERE concat(', ',course,',') like '%, 5,%'
This question already has answers here:
Opposite of MySQL FIND_IN_SET
(6 answers)
Closed 9 years ago.
I have a database which has a field containing some comma separated values like 1,8,3,54,5,19,9..... I want to select only those rows where 2 doesn't exists.
The query below is used for finding all fields containing the number 2 in the attachedCompanyIDs column. However, I want to find all rows where that number doesn't exist, but I don't know how to use find_in_set in this case. Can any one please help me?
SELECT name FROM company
WHERE orderID = 1 AND FIND_IN_SET(2, attachedCompanyIDs);
SELECT name FROM company
WHERE orderID = 1 AND NOT FIND_IN_SET(2, attachedCompanyIDs);
or
SELECT name FROM company
WHERE orderID = 1 AND FIND_IN_SET(2, attachedCompanyIDs) = 0;
This question already has answers here:
php/mysql add rows together to get total
(2 answers)
Closed 8 years ago.
I want to multiply the values on two columns... one column is named "point" and the other "unit". i actually want to multiply values on both column and then sum the total of the multiplied values. how do i go about this?
I hope you lookin something like this..
SELECT sum(total) FROM
(
SELECT col1 * col2 AS total FROM your_table tbl1
) tbl2
Just use SUM() aggregate on a product of point and unit
SELECT SUM(point * unit) total
FROM Table1
Here is SQLFiddle demo