How to SUM two columns in a SELECT query in mysqli [duplicate] - php

This question already has answers here:
Sum Two Columns in Two Mysql Tables
(2 answers)
MySQL SUM multiple columns
(1 answer)
Closed 4 years ago.
So my question is how to SUM two columns in a SELECT query in MySQL.Here is the code I have written:
$qry = "SELECT supplier,
SUM(pay) as paid,
SUM(remaining) as remain
FROM managment
WHERE supplier = '".$_GET['ssupplier']."'";
But it only plus the column named remaining but not the pay columns.I have 2 pay columns with two values (10 and 50) but after SUM the column I am getting 1050 instead 60.Kindly take a look and help me with this problem.I want this in one table, not two tables
Thanks

Related

How i join two mysql table using php? [duplicate]

This question already has answers here:
How to join two tables mysql?
(4 answers)
Closed 5 years ago.
I have two table in mysql database. Database name frschool. First table name 'profile' fields id, invoice,name and second table name is 'exam' fields id, invoice, obt.mark.
Now how i join the tables for showing data in a one page .
If you want to join 'profile' table with 'exam' table you need to have in 'exam' table also the field idProfile or some field to make a connection between the 2 tables

SQL Select -- Checking if value is in a comma separated list -- PHP [duplicate]

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,%'

How to display last 3-5 records from database? [duplicate]

This question already has answers here:
How to select last N records from a table in mysql
(4 answers)
Closed 8 years ago.
How can I display the last 3 or 5 records in my case posts (my columns in database are post_id, post_title, post_date, post_text) in HTML page but in table?
I suppose I know to display records in table but how to edit style of the table, where? I am talking about displaying just last 3 or 5 records not all records from database.
I suppose something like this. Naturally, replace table with your table. Also this question has been asked a million times before here.
SELECT * FROM (
SELECT * FROM table ORDER BY post_id DESC LIMIT 5
)
ORDER BY post_id ASC;

Finding a specific number within a comma separated list [duplicate]

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;

PHP MYSQL: How to add values from different columns [duplicate]

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

Categories