Sum values in multidimensional array (selecting multiple columns with multiple rows) - php

Let's say I have 5 different columns, a, b, c, d, e, and I'm selecting multiple rows:
$result = mysqli_query($conn,"SELECT a,b,c,d,e FROM posts WHERE submitter='$user'");
while ($row = mysqli_fetch_assoc($result)){
$ratings[] = $row;
}
Example:
The user has 3 posts, so it'll select 3 rows in the query.
I want to sum all of the rows' values for a (and the rest of course).
e.g.
row 1 a value = 4
row 2 a value = 10
row 3 a value = 1
So I need to sum all of those to get 15.
I know to use array_sum($ratings) to find the sum of the array but only if you select one column (a) which can have multiple rows, but this is multi-dimensional right due to multiple column values being selected?

You can just use sum in your query:
select sum(a)
, sum(b)
, sum(c)
, sum(d)
, sum(e)
from posts
where submitter = '$user'

You can use count aggregate function and group by in MySQL.
SELECT
submitter,
count(a) as rating
FROM posts
WHERE submitter='$user'
GROUP BY submitter
A a result you will get something like that:
some submitter, 3
another submitter, 10
one more submitter, 1
Is this helpful?

Related

How to fetch multiple values from the table based on foreign key?

I have two tables,
#table 1 contains the following data structure
NO category_name category_code amount type
1 provident_fund PF 1000 Deduction
2 Home Allowance HM 12000 Earning
3 Basic_pay BP 35000 Earning
#table 2 contains the following data structure
NO group_name payroll_cat_name
1 permanent PF,HM,BP,TX
1 visiting HM,BP
from the table 2, I can get the value of payroll_cat_name using the query
$a = "Select payroll_cat_name from table_2";
using the above query, I'm getting the value PF,HM,BP,TX.
Now, I have to use this multiple values PF,HM,BP,TX in order to get the sum amount from table 1.
Following is my code that I have tried,
include "../db.php";
$a = "Select payroll_cat_name from table_2";
$b = mysqli_query($con,$a);
$c = mysqli_fetch_array($b);
$d = $c["payroll_cat_name"];
echo "$d";
$myArray = explode(',', $d);
print_r($myArray);
$tr = "select SUM(amount) as am from table_1 where category_code in ($d)";
$rt = mysqli_query($con,$tr);
$new = mysqli_fetch_array($rt);
$gh = $new["am"];
That’s a poor data model. There should be a separate table to store the relation between groups and categories, which each tuple on a different row. The linked commented by sticky bit on the question gives great explanation on how and why.
For your current structure, one option using uses find_in_set() and a subquery:
select t2.*,
(
select sum(t1.amount)
from table_1 t1
where find_in_set(t1.category_code, t2.payroll_cat_name)
) as t1_amount
from table_t2 t2

How select next row pagination in sql

I'm sorry I'm weak for English.
i echo 2 row in each page . how echo next 2 row
SELECT *
FROM `mzmx_post`
JOIN mzmx_post_category
WHERE mzmx_post.id = mzmx_post_category.post_id AND zmx_post_category.category_id = 5
ORDER BY id DESC
LIMIT 2
You can use the two-arguments form of LIMIT to offset the result by a given number of rows, like:
SELECT *
FROM `mzmx_post`
JOIN mzmx_post_category ON mzmx_post.id = mzmx_post_category.post_id
WHERE mzmx_post_category.category_id = 5
ORDER BY id DESC
LIMIT 2, 2 -- fetch records 3 and 4
This gives you the second page. If you want the third page, then:
LIMIT 4, 2
And so on.
Note that I modified your query so the joining condition between the tables is placed in the ON clause of the join rather than in the WHERE clause.
Better add one extra column (e.g. mzmx_post_key bigint) of Long type in each table and have sequential value on that column. Use that column to fetch data from DB from page wise.
sqL suery should look like:
SELECT *
FROM `mzmx_post`
JOIN mzmx_post_category ON mzmx_post.id = mzmx_post_category.post_id
WHERE mzmx_post_category.category_id = 5 and mzmx_post_key> ##last record key##
ORDER BY mzmx_post_key ASC
LIMIT 2
The basic idea is to use
LIMIT n,o
where n is the results per page
o is the offset from the first result
for the p-th page the offset would be
o = p * n
where p = 0,1,2,....

Select rows from 2 tables in 1 query

Need to select name,domain from table 1, than I need a sum of values from a column from table 2 with condition table1.id = table2.
Table 2 does not have the same number of columns.
I have tried joining 2 queries with UNION and UNION ALL, but i keep getting the same problem of different number of columns.
$rows = $test->query('select domain,name from customers
UNION
select SUM(customer_id) from main.rentals,main.customers where customer_id = customers.id');
Expected would be "User's Name" - "domain" - "number of rentals(integer)"
Warning: SQLite3::query(): Unable to prepare statement: 1, SELECTs to the left and right of UNION do not have the same number of result columns
If you use SUM, it will sum all ids, for example if you have a customer ID of 10 and it shows 5 times the result will be 50. In the other hand if you use count it will count the rows that that id was shown. That's why we are doing a group by. You may tweak it to fill your specific needs, but this is one way to achieve what you want.
$query = "
SELECT
domain,
name,
count(customer_id) as Total
FROM
customers
left join main.rentals on customers.id = customer_id
GROUP BY
customer_id
";
$test->query($query );

Select count for each distinct row (mysql and php)

I am using mysql and php.
I have a table with one column. I can show unique rows by:
select distinct id from id_table
This might show
1
2
3
5
What I want to do is show the number of 1's, 2's, etc there are.
I could do
select count(id) from id_table where id = 1
But then I have to run a query for every... single... row... And with hundreds of thousands of rows, not good.
Is there a way to return an array of the counts of each distinct item
Data
1, 1, 1, 2, 3, 3, 5
Results
3, 1, 2, 1
Thanks
select id, count(id)
from table
group by id
If only want count of ids, then
select count(id)
from table
group by id
Here is a simple tutorial of group by clause
http://www.w3schools.com/sql/sql_groupby.asp

Multiple SELECT, Avoid duplicate results for one table in MySQL query

How can I select from two tables where table 1 returns 1 row and table 2 returns multiple rows resulting in NO duplicates from table 1?
Below, page.title has one row. page_images.image_loc can have anything from 1 to 10. If page_images.image_loc has 2 or more, page.title will be duplicated for the count of page_images.image_loc rows. How can I limit page.title to 1, but not page_images.image_loc?
$query = mysql_query("
SELECT page.title,
page_images.image_loc
FROM page, page_images
WHERE page.url_category = '$category' AND
page.url_title = page_images.page_title
");
You could try SELECT page.title, GROUP_CONCAT(page_images.image_loc) AS image_locs ... GROUP BY page.title to collect all the image_loc values in one row, then separate the list with explode().

Categories