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

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

Related

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

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

How do I delete duplicate rows in SQL? [duplicate]

This question already has answers here:
Remove duplicate rows in MySQL
(26 answers)
Closed 7 years ago.
I am trying to delete duplicate rows using SQL, whilst leaving one of the rows behind.
The table I am trying to delete duplicate rows from is called table "A" made up of:
A.AID, A.BID, A.AName, A.AType, A.APrice.
In this table I have a number of duplicate rows with all of the data exactly the same apart from the A.ID.
I am trying to create a query that will look for duplicates and then remove the duplicate making sure one of the rows are left behind. I am using phpMyAdmin and MySQL.
DELETE FROM member
WHERE id IN (SELECT *
FROM (SELECT id FROM member
GROUP BY member_id, quiz_num, question_num, answer_num HAVING (COUNT(*) > 1)
) AS A
);
use group by and count
DELETE FROM
YourTable
WHERE
AID NOT IN ( SELECT
MAX(AID)
FROM
YourTable
GROUP BY
BID ,
AName ,
AType ,
APrice );
Consider the query below, this will remove all the duplicate rows and prevents any future duplicate row.
ALTER IGNORE TABLE A ADD UNIQUE INDEX index_name (A.AID, A.BID, A.AName, A.AType, A.APrice );

Is there have the best way to count all rows in table mysql? [duplicate]

This question already has answers here:
MySQL: Fastest way to count number of rows
(14 answers)
Closed 8 years ago.
I want to count all of rows in table that match to my condition as fast as mysql can do.
So, i have four SQL and want you to explain all of them, how is it different for each SQL?
and which is fastest or best for query times and server performance? or it has another way that can better than these. Thank you.
select count(*) as total from table where my_condition
select count(1) as total from table where my_condition
select count(primary_key) as total from table where my_condition
or
select * from table where my_condition
//and then use mysql_num_rows()
First of all don't even think about doing the last one! It literally means select all the data I have in the database, return it to the client and the client will count them!
Second, you need to understand that if you're counting by column name, count will not return null values, for example: select count(column1) from table_name; if there is a row where column1 is null, it will not count it, so be careful.
select count(*), select count(1), select count(primary_key) are all equal and you'll see no difference whatsoever.

Sum of the columns mysql [duplicate]

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;

Get number of rows in MySQL [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Fastest way to count exact number of rows in a very large table?
What query do I use to get the number of rows that share a common specification.
Example: The number of rows where idpost = 3
You can use COUNT() documented here.
SELECT COUNT(1) FROM posts WHERE idpost = 3
EDIT: Updated according to dbf's suggestion.. Make sure you distinguish between COUNT(*) and COUNT(1), discussed here.
The query looks like this:
SELECT count(*) FROM some_table WHERE (conditions);
In your example:
SELECT count(*) FROM some_table WHERE idpost=3;
More on counting rows in MySQL: MySQL 5.6 Reference Manual: 3.3.4.8. Counting Rows
EDIT:
If you were wondering, which way of counting all rows is better (count(*) or count(1)), see this: What is better in MYSQL count(*) or count(1)?.
Try
SELECT
COUNT(*) as NumberRows
FROM
your_table_name_here
WHERE
idpost = 3
;
Consider learning SQL:
select count(*) from mytable where idpost=3
the select count(*) function grabs the number of rows that meet a certain criteria - define that in the where statement.
http://dev.mysql.com/doc/refman/5.1/en/counting-rows.html - read here

Categories