How to GROUP_CONTCAT a COUNT in mysql? - php

I have a mysql query that counts all website visitors and groups them by month. So if I run the query, I get 12 rows with the visitors for each month. I want to have them in a php string separated by a comma. I read that this is possible with GROUP_CONCAT. But I don't know how to implement this function in my query.
Here is my query:
SELECT COUNT(count_id) FROM counter GROUP BY count_month ORDER BY count_month;
Does somebody know how to do this?
Any help would be appreciated.

Use your query as a subquery
select group_concat( my_count)
from (
SELECT COUNT(count_id) my_count
FROM counter
GROUP BY count_month
ORDER BY count_month
) t

Related

Count Number of Elements From a DISTINCT query

I have this tbl_transactions, this is the table
the image is the result after querying
SELECT DISTINCT vehicle_type_name, vehicle_plate_number FROM tbl_transactions
now, I need to count the number of 2 Wheeler, 4 Wheeler, etc... can someone help me to this. I've used the combination of DISTINCT and WHERE clause, but it shows error in query.
You can select in a subquery only the distinct values and count it on the outer query. If this is what you are trying please try and let me know if it helps and feel free to ask if something is unclear.
SELECT vehicle_plate_number,count(*) as number_of_rows
FROM (
SELECT vehicle_type_name,
vehicle_plate_number
FROM tbl_transactions
GROUP BY vehicle_plate_number,vehicle_type_name
) as t1
GROUP BY vehicle_plate_number;
Demo: https://www.db-fiddle.com/f/vwi9bkdUVPoZeURdNAEoDX/4

SQL Select the column if the value is like with multiple columns

I need to create a autocomplete search with ajax. The suggestions should only contain the 10 most entered results. The search query has to check multiple columns if the value is like my variable.
But my problem is to create the query and the php logic for that.
Is there any plugin or something simular for that?
How can I select a column if the value in it is like my variable?
I need to create a count query, which counts (in all columns) "how often is here the full word (splitted by spaces)" <- which is like the found one (to get the relevance)
At the end I need to sort the found entries by their relevance to provide the 10 most relevant entries.
(The real query checks for more columns than just 2, but for dummy reasons are 2 okay)
The query which selects the rows where the value is like...
select * from
(
(select department from entries where department like '%myVariable%')
OR
(select grade from entries where grade like '%myVariable%')
)
I think you know what I mean. Does anyone have any hints, suggestions, examples or useful links for me?
Thanks in advance!
Best regards,
FriaN
Why not use union all here?
select department from entries where department like '%myVariable%'
union all
select grade from entries where grade like '%myVariable%'
Then this should order the results for you:
select department, count(*) cnt from (
select department from entries where department like '%myVariable%'
union all
select grade from entries where grade like '%myVariable%')a
group by department
order by count(*) desc

How to get only 1 row of each similar content?

I Have a private message system on my site and i'm trying to pull all subject's on the messages.
I need to show any subject once.
so if I have this subjects:
hey
hello
hey
good morning
good morning
I need to print this:
hey
hello
good morning
I can just cross it out with if, but I guess there is a better way with sql.
Thank you.
Option 1 : DISTINCT
SELECT DISTINCT subject
FROM my_table
Option 2 : GROUP BY
SELECT subjects
FROM my_table
GROUP BY subjects
Difference between GROUP BY and DISTINCT
Distinct is used to filter unique records out of the records that satisfy the query criteria.
Group by clause is used to group the data upon which the aggregate functions are fired and the output is returned based on the columns in the group by clause. It has its own limitations such as all the columns that are in the select query apart from the aggregate functions have to be the part of the Group by clause.
See this and this for the reference .
Try something like:
SELECT DISTINCT subject FROM emails;
If you use SQL.
select distinct subjects
from YOUR_TABLE.
or
Select subjects
from YOU_TABLE
group by subjects

PHP/MySQL - Counting items within a query

I have the following results for my database table:
The Query:
SELECT
service_titles.user_id, service_titles.slide_id, service_titles.name as title_name ,service_names.name as service_name
FROM service_names
INNER JOIN service_titles ON service_names.title_id = service_titles.id
So what needs to happen is:
If the user has 2 unique service titles, then the max number of service_names for that title will be 6
If the user has 1 service title, the the max number of service_names for that title will be 16
I will be using PHP for all of the coding, but I am wondering how I would go about this. I need a way to count how many unique service_titles there are for that user and slide, and then count how many service items there are for each title.
Thanks for any help!
SELECT COUNT(DISTINCT service_titles.name)
FROM service_names
INNER JOIN service_titles ON service_names.title_id = service_titles.id
GROUP BY service_titles.user_id, service_titles.slide_id
That'll get you the number of distinct title_names for each user_id/slide_id combo.
SELECT COUNT(DISTINCT service_names.name)
FROM service_names
INNER JOIN service_titles ON service_names.title_id = service_titles.id
GROUP BY service_titles.user_id, service_titles.slide_id
... and that's the number of distinct service_names for same. If you want both in one query, you can put both COUNTs together, since you're using the same GROUP BY regardless.
You could use a CASE statement within your query to change the max number of service_names.
See MySQL CASE statement reference
To do this in the SQL itself would be quicker than evaluating it in PHP.
To count how many distinct titles you can try:
SELECT user_id, COUNT(DISTINCT name)
FROM service_titles
GROUP BY user_id

mySQL query to count number of unique users posting to database

In a database we have about 1000 registered users. I'd like to know how many of those users have actually written a question or posted an answer. All the information can be take from the tblQA table and the userid is "intPosterID", the questions & answers each have their own ID, "PostID". Is there a query that can be run to count how many unique users have posted a question or answer?
Counting the distinct userIDs can be done via:
SELECT COUNT( DISTINCT intPosterID ) FROM tblQA;
COUNT( DISTINCT field ) returns a count of the number of rows with different values for the given field - intPosterID in this case.
Count posts per user :
SELECT COUNT(PostID), intPosterID FROM tblQA GROUP BY intPosterId
numbers of results = number of users or run ConroyP query
COUNT(DISTINCT columnname) can be used for that :
SELECT COUNT(DISTINCT intPosterId) FROM tblQA;
This should do it.
select count(intPosterID)
from tblQA
group by intPosterID;

Categories