MySQL query help on joins and counting most occurance - mysql

I have two tables, client_table and order_table
Entries in the client_table are unique with id as primary key
In the order_table, the order_id is the primary key and also a client_id field. The client_id is the primary key of the client_table.
There can be multiple entries in the order_table that contain the same client_id. So it is a one-to-many relationship.
I'm trying to come up with a query that produce the most occurring client_id in the order_table
I have tried the following queries.
SELECT a.id FROM `client_table` as a inner join order_table as b GROUP BY a.id ORDER BY count(b.client_id)
So I'm looking for a result of client_id which have the most orders. I suppose I only need the order_table and don't need the client_table at all right ?

SELECT b.client_id, count(*) AS Clients
FROM order_table AS b
GROUP BY b.client_id
ORDER BY Clients DESC
No need for a join if you only want the clients id, like you said. Also, if you want to join, like in your example query, you need to specify on which column with a ON clause.
... inner join order_table as b on a.id=b.client_id ...

Yes, you don't need the client table at all:
select
client_id,
count(order_id) order_count
from
order_table
group by
client_id
order by
count(order_id) DESC

you can try:
SELECT a.id FROM `client_table` as a left join order_table as b ON a.id = b.client_id GROUP BY a.id ORDER BY count(b.client_id) desc
Use DESC at ORDER BY to get most occurances. Right now you only get the lowest occurances, and try changing INNER JOIN to LEFT JOIN.

Related

MySql SELECT * FROM Table A and JOIN Column FROM Table B WHERE LOG_ID Match

I need SQL Query i mention details in sample image, I hope you understand
Thank you
Try this query and let us know if it works for you:-
Select a.*, b.degree from table_a as a inner join table_b as b on
b.log_id=a.log_id order by a.log_id asc
In this database query we have performed an inner join between the two tables and matched it based on log_id and at last order it in ascending order based on log_id itself.
This should work:
SELECT a.*, b.degree
FROM a
LEFT JOIN b ON a.s_id = b.n_id
WHERE a.log_id = 102
ORDER BY a.log_id ASC;
Remember to change the table names a and b to the correct table names. More on this topic here.
This code works:
Select a.*, b.degree from table_a as a inner join table_b as b on b.log_id=a.log_id where a.log_id="102" order by a.log_id asc;
Thanks you all

MySQL - Return the last record on the second table then return all in the first table

I have two tables customers and orders, below is the structure.
Table - contacts
id
Table - orders
id
contact_id
How can I select all from contacts table but only select the latest record from the orders table?
SELECT contacts.*,
Max(orders.id)
FROM contacts
LEFT JOIN orders
ON contacts.id = orders.contact_id
GROUP BY contacts.id;
But I always gets NULL if I use LEFT JOIN, it only have value if I use INNER JOIN.
select the latest record in orders and group it first
select contacts.*, orders.id
from contacts
left join (select max(id) as id, contact_id
from orders
group by contact_id) orders
on contacts.id = orders.contact_id
You can try to use UNION like
select * from orders order by id desc limit 1
UNION
select * from contacts
In order to aggregate max value with all columns from contacts table, add all columns from contacts table after group by function
I trust the answer provided by Alex should work well. The following query shall list all records from contacts and the last id from orders table.
SELECT
c.*,
(SELECT Max(o.id) FROM orders o
INNER JOIN contacts c1 ON o.id=c1.id
)as last_order_id
FROM contacts c

MySQL to calculate SUM with foreign key relation

I have two tables 1.material_line_item and 2.item_master there is a foreign key relation between two tables (item_master_id present in material_line_item) and there is a column called item_code in item_master. So I want a Join Query to display the item_code with my current query.
I would approach this by joining the item_master table to a subquery of material_line_item which calculates the aggregates you want for each item master id value. I am selecting all columns available though you are free to choose whichever columns you want.
SELECT t1.*, t2.*
FROM item_master t1
INNER JOIN
(
SELECT item_master_id,
SUM(received_quantity) AS Total_Received_Qty,
SUM(ordered_quantity) AS Total_Ordered_Qty
FROM material_line_item
GROUP BY item_master_id
) t2
ON t1.id = t2.item_master_id
you can simply join the two tables like
select item.item_master_id, master.item_id, Sum(received_quantity),
sum(ordered_quantity) from material_line_item item
left join item_master master on item.item_master_id = master.id
group by item.item_master_id, master.item_id
Try this,
select a.item_master_id,a.Total_received_qty,a.Total_ordered_Qty,b.item_code from (select item_master_id,
sum(received_quantity) Total_received_qty,
sum(ordered_quantity) Total_ordered_Qty
from material_line_item
group by item_master_id) a, item_master b where a.item_master_id =b.item_master_id
Hope it will help.

Selecting rows with unique field values in mysql

I have these columns for table comments:
id
content
add_date
uid
school_id
Rows can have the same school_id.
I want to select the latest data according to add_date, but only 1 row per school_id (no duplicate for school_id) with limit of 10.
I've tried many codes already and its not working for me.
Any help would be appreciated.
This is what we call Greatest N per Group. You can achieved this by putting into a subquery so it can be joined against the non-grouped table (comments).
Try this:
SELECT c.*
FROM
(
SELECT school_id, MAX(add_date) maxDate
FROM comments
GROUP BY school_id
) x INNER JOIN comments c
ON x.school_id = c.school_ID AND
x.maxDate = c.add_date
ORDER BY x.maxDate desc
LIMIT 10
select C.ID, C.Content, t1.MaxDate as [add_date], C.uid, t1.school_id
from (selet school_id, max(add_Date) as 'MaxDate'
from comments
group by school_id) T1
inner join comments C on T1.school_id = C.school_id and C.add_Date= T1.MaxDate
LIMIT 10
If you want to choose which 10 rows return, add an order by, or a Where clause
select c1.*
from comments c1
where add_date = (select max(add_date) from comments c2 where c2.school_id =c1.school_id)
order by add_date desc
limit 10
create indexes on comments(add_date) and comments(school_id, add_date)

#1060 - Duplicate column name 'id'

Why I get #1060 - Duplicate column name 'id'
SELECT COUNT(*) FROM (SELECT * FROM `tips` `t` LEFT JOIN
tip_usage ON tip_usage.tip_id=t.id GROUP BY t.id) sq
Probably because the * in select * selects two columns with the same name from tip_usage and tips.
Probably it's because the inner select yields two columns with the name id. Since you are not using those columns, you can just change the select to:
SELECT COUNT(*) FROM (SELECT t.id FROM `tips` `t`
LEFT JOIN tip_usage ON tip_usage.tip_id=t.id
GROUP BY t.id) sq
Your query is equivalent to this:
SELECT COUNT(DISTINCT id)
FROM tips
, there is no need in a join.
Are you sure you didn't want an INNER JOIN instead?
Had the same problem, renaming into select clause saved me
SELECT people.id, vehicle.id ...
I renamed it with AS keyword
SELECT people.id AS person_id, vehicle.id ...