Select count for each distinct row (mysql and php) - 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

Related

SELECT * FROM table WHERE (my_id) IN table_column (assigned_id)

I have a table called tasks Where has a column named assigned_id which has comma separated values like (1, 2, 3, 4, 5). I need to count of the tasks user has assigned.
In Example: My name is Imdad and my user_id is 5, and the tasks table has column assigned_id which values are like 1, 3, 4, 5, 7... I need to count all the number of rows which assigned_id contain my user_id which is 5.
I have tried so many methods but not working till now.
I have tried
SELECT * FROM tasks WHERE $user_id IN (assigned_id);
Here the user_id is 5
It's returning if the assign_id value start with 5 (Example: 5, 3, 6, 7...)
Here is my code
<?php
$data = "SELECT COUNT('1') FROM task WHERE $user[id] IN (assigned_id)";
$get_data = mysqli_query($conn, $data);
$show = mysqli_fetch_array($get_data);
echo $show[0];
?>
I am attaching a screenshot for better understand!!!
Here I have marked the data which I want
But Here is the image with query, I got the first value
As per my understanding on the above query this should work for you in MYSQL
if you want to get based on task_title
Select count(*) from tasks where assignee_id LIKE (CONCAT('%', (Select id from tasks where task_title = 'Imdad' LIMIT 1), '%'));
if you want to get based on id
Select count(*) from tasks where assignee_id LIKE (CONCAT('%', (Select id from tasks where id = 5 LIMIT 1), '%'));
Hope this helps.
In such case you can use find_in_set function like:
select *
from tbl
where find_in_set(5, assigned_id);
SQL online editor

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 );

Split single row to multiple according to a number

Using the below select statement:
select
spec_sheet_color_comb.id,
spec_sheet_color_comb.pairs*2 as total
from
spec_sheet_color_comb
where
spec_sheet_color_comb.id_spec_sheet IN (4814)
And getting this result:
id total
79928 5
Now, I want to split this result according to the TOTAL quantity and get this result, 5 rows showing the ID:
Result
79928
79928
79928
79928
79928
It is really important, Thanks
Suppose you have a table having 2 columns id and total. (You can use your query instead of the table).
You need a collateral table (or view) with all the numbers 1..max_number_you_need
e.g.
(select 1 as number
union
select 2 as number
...
select 999 as number)
Then you can use the number source table joining your table you need to multiply rows
select id
from the_table t
join (table with numbers) n on n.number<=t.total
UPDATE: an example
SELECT id
from (select 1 as id, 5 as total
union
select 2 as id, 3 as total) t
join (select 1 as number
union
select 2 number
union
select 3 number
union
select 4 number
union
select 5 number) num on num.number<=t.total
order by id

find entries to the left and right of a certain entry in a mysql table in a single query

Let's say I have a table in a database like this table. Let's say that I want to get the entries on the left and right of the entry whose the primary key is equal to 5 (or any other primary key). So in our case, I want to get the entries whose primary key is equal to 4 and 6 respectively. What is the SQL query that will give me such result? Can you guys translate the SQL query into a find('all') CakePHP query?
Thank you
NOTE: The ids are not necessarily contiguous, meaning, they do not necessarily follow the 1, 2, 3, 4, 5, 6, 7, 8 sequence. I can have something like 1, 5, 13, 44, 66, 123, etc
Try Union like this
(SELECT * FROM employee WHERE id < 5 order by id DESC LIMIT 1)
UNION
(SELECT * FROM employee WHERE id >5 LIMIT 1)
PHP
$id = 5;
SELECT * FROM Employee where id = $id-1 OR id = $id+1;
MySQL
SET #id = 5;
SELECT * FROM Employee where id = #id-1 OR id = #id+1;
Checkout find('neighbors'). It returns the records before and after the one you specify and your ids can have "holes" in the sequence.

MySQL Group By + Order

I've got a problem with grouping my rows.
Example Table
ID, GroupID, INFO, COUNTER
1, 123456, INFO, 21
2, 654321, INFO, 20
3, 123456, INFO, 30
4, 654321, INFO, 11
First of all, I'd like to display them with this:
SELECT *
FROM table
GROUP BY GroupID
ORDER BY COUNTER DESC
LIMIT 0, 60
So it should display only one of group type. It does, but the order is not good. So I think it's not getting the right number of counter paired to the GroupID.
The right displayed result would be: (the main order selector has to be the count)
ID, GroupID, INFO, COUNTER
3, 123456, INFO, 30
2, 654321, INFO, 20
How should I solve?
SELECT tablename.* FROM tablename
WHERE tablename.COUNTER =
(SELECT MAX(COUNTER) FROM tablename AS f WHERE f.GroupID = tablename.GroupID)
ORDER BY tablename.COUNTER DESC
Edited : This will get you the complete rows containing the max value of COUNTER for each GroupID, and order the final results by COUNTER desc.

Categories