Count Number of Elements From a DISTINCT query - php

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

Related

How to GROUP_CONTCAT a COUNT in mysql?

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

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

Getting results from two tables by using SELECT Query

I have 2 MySQL tables:
I store the admin_vat_id in the Fac__Article table which is actually a reference to the id of the Fac__Admin_vat:
What I'm trying to do
I want to get all Fac__Article table's entries, but at the admin_vat_id column, where it normally would display the integer value, I want to display the float value of the column rate of the table Fac__Admin_vat.
I guess I have to use the select and union keyword, but I don't know how to implement this select query. Please guide me with knowledge in solving this problem.
All you need is a simple LEFT JOIN:
SELECT
fa.id,
fav.rate,
fa.article_number,
fa.name,
fa.description,
fa.unit,
fa.price,
fa.stock,
fa.stock_warning,
fa.visible
FROM `fac_article` fa
LEFT JOIN `fac_admin_vat` fav
ON fa.admin_vat_id = fav.id
SQL Fiddle
Try this (correct the table names if i misspelled): SELECT *, Fac__Admin_vat.rate FROM Fac__Article LEFT JOIN Fac__Admin_vat ON Fac__Admin_vat.id = Fac__Article.admin_vat_id
Try using left join, a rough example below.
SELECT *, Fac__Admin_vat.rate as admin_rate FROM Fac__Article LEFT JOIN Fac__Admin_vat ON Fac__Article.admin_vat_id = Fac__Admin_vat.id
This will allow you to use admin_rate in your code to get the data you want. Hope this helps.

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

Getting distinct values in this query

Hi, this is my query:
SELECT tbl_order_detail.order_id, tbl_order_lead_send_detail.customer_id, tbl_order_detail.order_title, tbl_order_detail.dealer_id , tbl_order_lead_send_detail.send_date_time
FROM tbl_order_detail
INNER JOIN tbl_order_lead_send_detail
ON tbl_order_detail.order_id=tbl_order_lead_send_detail.order_id
where tbl_order_detail.order_status='Active'
ORDER BY tbl_order_lead_send_detail.send_date_time DESC
I am getting this output,
I want to get only one data-row for one means distinct value of Order ID. How can I change my sql query to get my desired result?
SELECT distinct(tbl_order_detail.order_id), tbl_order_lead_send_detail.customer_id, tbl_order_detail.order_title, tbl_order_detail.dealer_id , tbl_order_lead_send_detail.send_date_time
FROM tbl_order_detail
INNER JOIN tbl_order_lead_send_detail
ON tbl_order_detail.order_id=tbl_order_lead_send_detail.order_id
where tbl_order_detail.order_status='Active'
ORDER BY tbl_order_lead_send_detail.send_date_time DESC
SELECT DISTINCT tbl_order_detail.order_id, ...
Two possibilities:
1) Select distinct ..
2) Select ... group by tbl_order_detail.order_id, tbl_order_lead_send_detail.customer_id, tbl_order_detail.order_title, tbl_order_detail.dealer_id , tbl_order_lead_send_detail.send_date_time
Be aware that even if you use the keyword distinct in your query, it'll still return more than one rows for the same order id if at least one of the columns return a different data.
Your result image shows 4 columns, while your query asked for 5; so, it's not 100% possible to determine where the problem lies.
That being said, use select distinct, and see if it solves your problem. If it doesn't, you might have to remove the column(s) with the different data from the query.
Happy coding!
From some of your screenshots it appears that customer id is different for each row. You need to show all the output to get sensible answers.
I'm pretty certain SELECT DISTINCT isn't broken, so you must be selecting non-unique rows.

Categories