Using LIMIT to get AVG from 2 tables - mysql

I currently have a query that gets every product_id FROM the receive_pallet TABLE. Along with every id it also gets the AVG of the date between when it is ordered/received (date_entered from the TABLE receive_pallet and date_ordered from the TABLE cs_po)
SELECT a.product_id, avg(a.date) AS AVG
FROM (SELECT DATEDIFF(date_entered,date_ordered) AS date,po_number_full, product_id
FROM cs_po,receive_pallet
WHERE cs_po.id = receive_pallet.po_number_full
ORDER BY cs_po.id DESC )a
GROUP BY a.product_id;
The result would be something like:
product_id AVG
00010005.01S 25.2500
00010005.04D 19.0000
00010010.01S 21.2680
00010020.02S 15.1250
00010040.04S 12.2400
00010080.20S 16.6667
This query works, but i would like to LIMIT it to AVG the last 5 shipments, not all of them. This is the query i made to do so,
SELECT a.product_id, avg(a.date) AS AVG
FROM (SELECT DATEDIFF(date_entered,date_ordered) AS date,po_number_full, product_id
FROM cs_po,receive_pallet
WHERE cs_po.id = receive_pallet.po_number_full
AND product_id IN (SELECT product_id , date_entered, date_ordered FROM receive_pallet LIMIT 0,5)
ORDER BY cs_po.id DESC
)a
GROUP BY a.product_id;
I get this error when i try to test it. [Err] 1235 - This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME sub query'
I know i need to replace the IN() clause with an INNER JOIN as LIMIT simply isn't supported in an IN() sub query, But i don't know where to start if i have two sub queries/where to add the join. Thanks.

Tale a look at this:
SELECT
a.product_id,
AVG(a.date) AS AVG
FROM (
SELECT
DATEDIFF(date_entered, date_ordered) AS date,
product_id
FROM
cs_po c
JOIN (
SELECT
date_entered,
product_id,
po_number_full
FROM
receive_pallet
ORDER BY
date_entered DESC
LIMIT 5
) r ON c.id = r.po_number_full
ORDER BY
c.id DESC
) a
GROUP BY
a.product_id
;
I simplified it a bit, but I think it works the way you want.

Related

Fast Query mysql between 3 tables

Help me guys, how to make my query faster
SELECT b.id_barang
, b.nama
, ( SELECT SUM(qty) FROM baru_kasir WHERE k.id_barang = b.id_barang GROUP BY b.id_barang LIMIT 1)
- ( SELECT SUM(qty) FROM barang_gdg_pesan WHERE barang_gdg_pesan.id_barang = b.id_barang GROUP BY b.id_barang LIMIT 1 ) AS STOCK
FROM baru_barang b
WHERE stts = 'AKTIF'
ORDER
BY b.nama ASC;
It will almost certainly be more efficient to JOIN to tables of the SUMs rather than running two subqueries for every row:
SELECT bb.id_barang,
bb.nama,
bk.qty - bgp.qty AS stock
FROM baru_barang bb
JOIN (SELECT id_barang, SUM(qty) AS qty
FROM baru_kasir
GROUP BY id_barang) bk ON bk.id_barang= bb.id_barang
JOIN (SELECT id_barang, SUM(qty) AS qty
FROM barang_gdg_pesan
GROUP BY id_barang) bgp ON bgp.id_barang = bb.id_barang
WHERE stts='AKTIF'
ORDER BY bb.nama ASC;
Ensuring that you have indexes on the id_barang columns in each table will also help performance.

how to enhance efficiency of my query

I have such a query:
SELECT
*,
(
SELECT COUNT(DISTINCT client_id)
FROM 1097_course_students_tbl
WHERE course_cycl_id=id
AND stts_id <> 8
AND client_id IN(SELECT id FROM 1097_clients_tbl WHERE is_removed=0)
) AS cnt
FROM 1097_course_cycle_tbl
WHERE (course_id IN (SELECT id FROM 1097_courses_tbl WHERE is_removed=2))
ORDER BY start_date DESC
I need to make it more efficient because it takes too long
any suggestions ?
thanks
Try the following
SELECT cc.*,IFNULL(q.cnt,0) cnt
FROM 1097_course_cycle_tbl cс
JOIN 1097_courses_tbl с ON c.id=cc.course_id AND c.is_removed=2
LEFT JOIN
(
SELECT cs.course_cycl_id,COUNT(DISTINCT cs.client_id) cnt
FROM 1097_course_students_tbl cs
JOIN 1097_clients_tbl c ON cs.client_id=c.id AND c.is_removed=0
WHERE cs.stts_id<>8
GROUP BY cs.course_cycl_id
) q
ON q.course_cycl_id=cс.id
ORDER BY cc.start_date DESC
I think id in 1097_courses_tbl and 1097_clients_tbl is primary key.
Therefore I replaced IN into JOIN.
And I converted the subquery from SELECT block wich executed for each rows into the subquery with GROUP BY which using in LEFT JOIN. Here it'll execute only one time and return all the necessary information.

SQL Adding count (from 2 tables) to existing select (of 3 tables)

Doing a query on forum database. I am using this query to get thread name, poster, date etc.
(Left only thread_subject for now)
SELECT `thread_subject` FROM `fusion_posts` JOIN `fusion_threads`
ON fusion_posts.thread_id=fusion_threads.thread_id JOIN `fusion_users` ON
fusion_posts.post_author=fusion_users.user_id
GROUP BY fusion_posts.thread_id ORDER BY `post_id` DESC LIMIT 16
Basically, I also need to add something like the count below to the existing select, to count posts of each thread.
SELECT COUNT(*) AS PostCount FROM fusion_posts,fusion_threads WHERE fusion_threads.thread_id = fusion_posts.thread_id group by fusion_threads.thread_id
How could I do that?
Try this:-
SELECT `thread_subject`, COUNT(*) AS PostCount
FROM `fusion_posts` JOIN `fusion_threads`
ON fusion_posts.thread_id=fusion_threads.thread_id JOIN `fusion_users`
ON fusion_posts.post_author=fusion_users.user_id
GROUP BY fusion_posts.thread_id, `thread_subject`
ORDER BY `post_id` DESC
LIMIT 16

Mysql- Select Query from two table

I have two table item_info and item_log tables are given below:
Now i need to search by 'like' item_name and the result will show last row of any item_id like:
can you try something like this:
SELECT table1.*, MAX( table1.id ) AS last_id,
table2.table2.id
FROM (table1)
JOIN table2 ON table1.table2.id = table2.table2.id
WHERE item_name LIKE '%nameOfItem%'
GROUP BY table1.table2.id
ORDER BY table1.id desc
it's kinda same with my function, i have to retrieve recent message for every thread. so i have to join the tables and group it by message id, and in the descending order in which the message has sent.
basically, the logic is quite the same. let me know if it doesn't work
Problem Solved
SELECT item_log.*,item_info.name
FROM (item_log)
JOIN item_infos ON item_log.item_id = item_infos.item_id
WHERE id IN (SELECT MAX( item_log.id )
FROM (item_log)
JOIN item_infos ON item_log.item_id = item_infos.item_id
WHERE item_name LIKE '%G%'
GROUP BY item_log.item_id
ORDER BY item_log.id desc)
AND item_name LIKE '%G%'
GROUP BY item_log.item_id
ORDER BY item_log.id desc
SELECT
*
FROM
(SELECT
item_info.`item_name`,
item_log.`item_id`,
item_log.`id`,
item_log.`item_barcode`,
item_log.`buy_price`
FROM
item_log,
item_info
WHERE item_info.`item_id` = item_log.`item_id`
ORDER BY id DESC) AS i
GROUP BY item_id ;
I think this is what you are expecting right ?
This may work for you:
SELECT l.id,i.item_id,l.item_barcode,i.item_name,l.buy_price
FROM item_info i
LEFT JOIN item_log l ON i.item_id=l.item_id
WHERE i.item_name LIKE '%G%'
ORDER BY l.id desc

How to avoid multiple results from sub query

Hi guys i am new bie to MySQL,this might be
easier to question but i am totally new for mysql.
i have two tables order and shops the Desc of two
tables look like this....
OrdersTable.
order id:
ordername:
shopnum(fk)
Shopstable*
shopname:
shopnum(pk):
i am using sub query like this,to get the shops name which have most number of orders....
like...19 xyzshop
. 13 hjjddshop
. 6 reebok shop
select shopname
from shopstable
where shopnum in
(select count(orderid) as highest ,shopnum
from orderTable
group by shopnum)
it throws error,display column should be 1,its because the subquery is returning 2 results...so how do i avoid that and get the appropriate result...help will be appreciated...:):)
It's not complaining because the subquery returns 2 results but two columns. But even if it did only return a single column, it would return 2 results and the main query would do the same.
No need for a subquery in any case:
SELECT s.shopname
FROM Shopstable s
JOIN OrdersTable o ON s.shopnum=o.shopnum
GROUP BY s.shopname
ORDER BY count(*) DESC
LIMIT 1
Use this:
select shopname
from shopstable
where shopnum in
(select shopnum
from orderTable
group by shopnum
order by count(*) DESC
limit 1)
remove count(orderid) as highest , in your query. you should only select one column in your subquery
i think you want something like this...
SELECT shopname, count(*) as highest
FROM shopstable
INNER JOIN orderTable ON (shopstable = orderTable.shopname)
GROUP BY shopstable.shopname
i think this is the result you want?