Total all occurrences of individual numbers in database table - php

I'm trying to total all occurrences of each individual number in a database table and return them in an associative array.
As an example, the table may consist of entries of: 1 3 4 2 1 2 1 1 4 3 3 4 2
When totaled, this would be 1 = 4, 2 = 3, 3 = 3, 4 = 3.
Ideally this would be returned as an array with keys of 'number' and 'total'.

The SQL code that you need is: SELECT num AS number, COUNT(num) AS occurrences FROM yourtable GROUP BY num
With the answer you will have at each row the num at the first field and the number of occurences at the second one. Then you can create an array and parte it just looping though the sql respones
Fetch the array would be something like(not tested code, just a guide):
$myArray = array();
while($row = mysql_fetch_array( $data ))
{
$myArray[$row["num"]] = $row["occurrences"];
}

One option is to fetch results and use array_count_values:
$arr = array_count_values($arr);
In SQL query you can do the same with COUNT and GROUP BY:
SELECT number, COUNT(*) as total FROM mytable GROUP BY number
The last will return two columns with corresponding data:
------------------
| number | total |
------------------
| 1 | 4 |
------------------
| 2 | 3 |
------------------
...

Related

sum all row from 2 column and use select to display two column

example
|like |comment|
|user1|asd |
|user2|awe |
|user3|aqw |
|user4|atr |
|user5|axc |
|user6|azw |
|user7| |
i have two column and what i wanted to do is to sum all rows inside those column and fetch the two column but i dont have any idea on how to do it in a query in sql.
result
|like |comment|
| 7 | 6 |
i tried queering it but i can only fetch 1 column i dont know how to do it with two column..
function countpostcomment($puid){
$sql = "SELECT COUNT(pc_comment) FROM pcomment WHERE post_uid = '$puid'";
$stmt = $this->dbh->prepare($sql);
$stmt->execute(array("0"));
$active_data = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $active_data;
}
here is my sample query i haven't tested the whole function only the select cause i dont know if i need to use array or not..
I think you simply want:
SELECT COUNT(*) as likes, COUNT(pc_comment) as comments
FROM pcomment
WHERE post_uid = '$puid';
COUNT(*) counts the number of rows (7 in this case). COUNT(<column name>) counts the number of non-NULL values in the column (6 in this case).

PHP + SQL -- Counting clicks in multiple rows

i stuck at the problem that my code wont count total clicks in multiple rows.
This is how the Database looks
id | jb_clicks | created_time
--------------------------------------------------
1 | 14 | 1475420816
2 | 7 | 1475422200
3 | 9 | 1475422217
4 | 3 | 1475422239
I want the result to be 33 (14+7+9+3)
How is this possible?
The current code looks like this:
SELECT COUNT(*) AS jb_clicks FROM jb_urls
my function:
function sumdatabase($select, $statement){
$config = new mysql_config;
$link = mysqli_connect($config::MYSQL_HOST,$config::MYSQL_USER,$config::MYSQL_PASS);
mysqli_select_db($link, $config::MYSQL_DATABASE);
$result = mysqli_query($link,"SELECT SUM(".$config::MYSQL_PREFIX."$select) AS jb_clicks FROM $statement");
$row = mysqli_fetch_assoc($link,$result);
return $row[$config::MYSQL_PREFIX.$select];
}
Use the following:
SELECT SUM(jb_clicks) AS jb_clicks FROM jb_urls
COUNT() is used to count the row numbers and SUM() is to calculate the total sum of a column in a table. In your case, it would be SUM(jb_clicks).

mysql get data by matching row of fixed position with strlength()

I have a table where a filed contains roll numbers like so:
id | roll_no
--------------
1 | 290320452
2 | 290320453
3 | 290340454
4 | 290330455
from the above column I want roll_no with matching position 4 AND 5 with 32.
where the result would be
id | roll_no
--------------
1 | 290320452
2 | 290320453
For now I have done by filtering the result array like so:
$subject_code = 32;
foreach($result as $row){
if($subject_code != NULL){
if(substr($row['roll_no'],3,2) == $subject_code){
$data[] = $row['roll_no'];
}
}
else{
$data[] = $row['roll_no'];
}
}
Is there a better way to filter while querying on mysql query.
I have seen SUBSTRING() function of mysql but no help.
select roll_no from your_table
where substring(cast(roll_no as char(11)), 4, 2) = '32'
How about this:
You can use _ (underscore) as wild card to match exact number of positions.
select
id, roll_no
from
roll_numbers_table
where
roll_no like '___32%'; // 3 underscores before 32
This statement on your sample data would result:
id | roll_no
--------------
1 | 290320452
2 | 290320453

How to parse results from MySQL Table with PHP using row data?

Typically when I make calls into a mysql db, I reference a column for values I need. In this particular instance however, I need to retrieve a set of rows and parse with PHP accordingly. The example is this:
Table format
ID | Level
-----------------
1 | 1
2 | 2
3 | 2
4 | 3
5 | 4
6 | 4
I am ultimately trying to retrieve all possible levels and count the number of results by those levels. A simple GROUP BY, COUNT() will do the trick:
'Select Level, Count(*) as counter FROM table GROUP BY Levels ORDER BY Levels ASC'
Which will return:
table_new
Level | Count
--------------
1 | 1
2 | 2
3 | 1
4 | 2
The problem I face though is when retrieving these results with PHP, I am not quite sure how to set a variable, say 'level1' and set it to the value returned in the count column.
I assume the logic would follow:
while ($row = $stmt->fetch()) {
$count_level = $row['counter']
}
(but then I would need to create counts for each level type. Any suggestions?
$level = array();
while ($row = $stmt->fetch()) {
$level[$row['level']] = $row['counter']
}
then you have $level array and $level[1], $level[2] etc variables

Count maximum times record appears in Database table

I am unable to find the proper mysql function but am trying to find the maximum number of a times a single record appears within a database relative to all other records.
For example:
ID | ....
================
1 | ....
2 | ....
2 | ....
2 | ....
3 | ....
3 | ....
the ideal return for what query i am trying to achieve is 3 (the count of 1 is 1, count of 2 is 3, count of 3 is 2 so return maximum count of ANY id).
Can't nest directly, otherwise you'll get a grouped max. Nest the selects instead.
select max(c) from (
select
count(*) c
group by
.. whatever ...
) x
SELECT MAX(MAX_COUNT) FROM (SELECT COUNT(COLUMN_NAME) AS MAX_COUNT FROM TABLE_NAME GROUP BY COLUMN_NAME)

Categories