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.
Related
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
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.
Can someone please help me in writing the exact query, which can give me desired results for the following,
I wrote,
SELECT tbl_order_detail.order_id,
tbl_order_detail.order_title,
tbl_order_detail.dealer_id
FROM tbl_order_detail
LEFT 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'
and finding the dealer name from one php function that takes dealer_id in it and returns dealer_name (using another mysql query), and count from another mysql query
but it isn't giving my desired output. it's giving output as
But I want the above circled ones in one row only, as everything is same in them, but they are showing many times in the output (why not one).
Can someone help me in this?
If you GROUP BY order_id, you should be good to go
Edit:
To get the dealer name, just JOIN the dealer table and select the dealer name column.
Untested, may contain errors
SELECT
tbl_order_detail.order_id,
tbl_order_detail.order_title,
dealer.dealer_name,
COUNT(tbl_order_lead_send_detail.order_id) AS total_customers_count
FROM tbl_order_detail
JOIN dealers ON (tbl_order_detail.dealer_id = dealers.dealer_id)
LEFT 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'
GROUP BY tbl_order_detail.order_id, tbl_order_detail.order_title, dealer.dealer_name;
To get a count, use COUNT(). To group your results by Order ID, use GROUP BY tbl_order_detail.order_id - one of the upsides of MySQL (or SQL in general) is that it's fairly close to plain English.
Try this:
SELECT OD.order_id,
OD.order_title,
OD.dealer_id,
SD.customer_id,
COUNT(SD.id) AS NumCustomers
FROM tbl_order_detail OD
LEFT JOIN tbl_order_lead_send_detail SD
ON OD.order_id=SD.order_id
WHERE OD.order_status='Active'
GROUP BY OD.order_id,
OD.order_title,
OD.dealer_id,
SD.customer_id
Can anyone please tell me why this result is generation only one results? taking in mind that everything is set right and the three tables are populated correctly, i took out the group_concat and it worked but of course with a php undefined index error!
SELECT
`songs`.`song_name`,
`songs`.`add_date`,
`songs`.`song_id`,
`songs`.`song_picture`,
group_concat(DISTINCT artists.artist_name) as artist_name
FROM (`songs`)
JOIN `mtm_songs_artists` ON `songs`.`song_id` = `mtm_songs_artists`.`song_id`
JOIN `artists` ON `artists`.`artist_id` = `mtm_songs_artists`.`artist_id`
ORDER BY `songs`.`song_id` DESC
LIMIT 10
so i'm guessing it's something related to group_concat.
best regards,
Rami
You're right, it's because of the group_concat. group_concat concatenates all items in the group... which is the entire result set if you have no GROUP BY clause. It's a bit hard to tell how to fix the query without the schema, but I think you need to either group by artist_id or use a subquery.
It should be concat not group_concat in my case.
my query:
SELECT events.*, SUM(analyt_event_hits.ajax_hits) AS ajax_hits
FROM events
LEFT JOIN analyt_event_hits
ON events.event_id = analyt_event_hits.event_id
WHERE events.date >= '$d'
the problem:
this is only returning one result,
when it should be returning many.
The query works fine if i remove SUM(anal_event_hits.ajax_hits) AS ajax_hits
I'm a bit of a MySQL novice so hopefully i'm missing something obvious!
Try adding
GROUP BY events.event_id
to the end.
If you use a group function like SUM in a statement containing no GROUP BY clause, it is equivalent to grouping on all rows.
and That's why you get only one row in results
More details on Aggregate Functions :
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html