Mysql- Select Query from two table - mysql

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

Related

Selection of the maximum deleted_data from duplicate rows

Table structure:
It is not possible to select records with the maximum delete date, which are grouped by one code, with the condition that they are all deleted. If the records have the same code, but have different delete statuses, then you do not need to select these records.
In this example, you select records with id = 3, id = 4.
SELECT * FROM analyzes_test WHERE code IN (SELECT code FROM analyzes_test GROUP BY code HAVING count(code)>1) AND deleted = (max deleted_date)
But I do not know how to substitute the longest possible date for deletion.
Please tell me who has more experience with sql.
Try the following simple query-:
select code,max(deleted_date) MAX_DeletedDate
from analyzes_test
group by code
having count(deleted_date)>1
you could use an inner join on max date group by code
select *
FROM analyzes_test a
inner join (
select code, max(deleted_date) max_date
FROM analyzes_test
group by code
) t on t.max_date = a.deleted_date and t.code = a.code
or if you don't want thw result for code with only a rows a could use
select *
FROM analyzes_test a
inner join (
select code, max(deleted_date) max_date
FROM analyzes_test
group by code
having count(*)>1
) t on t.max_date = a.deleted_date and t.code = a.code
TRY THIS: GROUP BY and HAVING will help to retrieve the max date of each code and use it as a subquery to retrieve all the information for the table as below:
SELECT ant.*
FROM analyzes_test ant
INNER JOIN (SELECT code, MAX(deleted_date) max_date
FROM analyzes_test
WHERE deleted_date IS NOT NULL
GROUP BY code
HAVING COUNT(deleted_date) > 1) t ON t.code = ant.code
AND t.max_date = ant.deleted_date

How to select first data of same id and sum column same id sql

I have data like
|id|id_barang|in|out|stock|
|1|1|2|0|2|
|2|1|2|0|4|
|3|1|0|1|3|
|4|2|2|0|2|
|5|2|1|0|3|
|6|2|0|1|2|
I want result is group by id_barang
|Id_barang|sum(in)|sum(out)|first(stock)|
|1|4|1|2|
|2|3|1|2|
You can use the MIN function for the first stock. It works similarly to SUM or COUNT.
You could try something like:
SELECT Id_barang, SUM(in), SUM(out), FIRST(stock)
FROM data
GROUP BY Id_barang
Unfortunately Mysql doesn't support Window functions. Try this
SELECT b.id_barang,
sum_in,
sum_out,
a.stock
FROM yourtable A
INNER JOIN (SELECT id_barang,
Sum(in) sum_in,
Sum(OUT) sum_out,
Min(id) AS min_id
FROM yourtable
GROUP BY id_barang) B
ON a.id_barang = b.id_barang
AND a.id = b.min_id
this query fetch all record of table group by Id_barang. its give first record of stock.
SELECT t1.Id_barang, sum(t1.in), sum(t1.out), (select min(t2.stock)
from table_name t2 where t2.Id_barang = t1.Id_barang)
FROM table_name t1
GROUP BY Id_barang
you can make query this way :
SELECT id_barang,SUM(in) as total_in,SUM(out) as total_out,stock
FROM stock
GROUP BY id_barang
if you found any error then you must change your column name, i think 'in' and 'out' is not valid. you change 'in' into 'in_change' and 'out' into 'out_change'. try with this column it works as you want. i did try....
SELECT id_barang,SUM(in_change) as total_in,SUM(out_change) as total_out,stock
FROM stock
GROUP BY id_barang

LEFT JOIN SUM with WHERE clause

The following query always outputs SUM for all rows instead of per userid. Not sure where else to look. Please help.
SELECT * FROM assignments
LEFT JOIN (
SELECT SUM(timeworked) AS totaltimeworked
FROM time_entries
) assignments ON (userid = assignments.userid AND ticketid = ?)
WHERE ticketid = ?
ORDER BY assigned,scheduled
If you want to keep the SELECT *, you would have to add a group by clause in the subquery. Something like this
SELECT * FROM assignments
LEFT JOIN (
SELECT SUM(timeworked) AS totaltimeworked
FROM time_entries
GROUP BY userid
) time_entriesSummed ON time_entriesSummed.userid = assignments.userid
WHERE ticketid = ?
ORDER BY assigned,scheduled
But a better way would be to change the SELECT * to instead select the fields you want a add a group by clause directly. Something like this
SELECT
assignments.id,
assignments.assigned,
assignments.scheduled,
SUM(time_entries.timeworked) AS totalTimeworked
FROM assignments
LEFT JOIN time_entries
ON time_entries.userid = assignments.userid
GROUP BY assignments.id, assignments.assigned, assignments.scheduled
Edit 1
Included table names in query 2 as mentioned in chameera's comment below

Using LIMIT to get AVG from 2 tables

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.

How do I get more than one column from a SELECT subquery?

Here is my problem :
I have 3 tables : account, account_event and account_subscription
account contains details like : company_name, email, phone, ...
account_event contains following events : incoming calls, outgoing calls, visit, mail
I use account_subscription in this query to retrieve the "prospects" accounts. If the account does not have a subscription, it is a prospect.
What I am using right now is the following query, which is working fine :
SELECT `account`.*,
(SELECT event_date
FROM clients.account_event cae
WHERE cae.account_id = account.id
AND cae.event_type = 'visit'
AND cae.event_done = 'Y'
ORDER BY event_date DESC
LIMIT 1) last_visit_date
FROM (`clients`.`account`)
WHERE (SELECT count(*)
FROM clients.account_subscription cas
WHERE cas.account_id = account.id) = 0
ORDER BY `last_visit_date` DESC
You can see that it returns the last_visit_date.
I would like to modify my query to return the last event details (last contact). I need the event_date AND the event_type.
So I tried the following query which is NOT working because apparently I can't get more than one column from my select subquery.
SELECT `account`.*,
(SELECT event_date last_contact_date, event_type last_contact_type
FROM clients.account_event cae
WHERE cae.account_id = account.id
AND cae.event_done = 'Y'
ORDER BY event_date DESC
LIMIT 1)
FROM (`clients`.`account`)
WHERE (SELECT count(*)
FROM clients.account_subscription cas
WHERE cas.account_id = account.id) = 0
ORDER BY `last_visit_date` DESC
I tried a lot of solutions around joins but my problem is that I need to get the last event for each account.
Any ideas?
Thank you in advance.
Jerome
Get a PRIMARY KEY in a subquery and join the actual table on it:
SELECT a.*, ae.*
FROM account a
JOIN account_event ae
ON ae.id =
(
SELECT id
FROM account_event aei
WHERE aei.account_id = a.id
AND aei.event_done = 'Y'
ORDER BY
event_date DESC
LIMIT 1
)
WHERE a.id NOT IN
(
SELECT account_id
FROM account_subscription
)
ORDER BY
last_visit_date DESC
Try moving the subquery to from part and alias it; it will look as just another table and you'll be able to extract more than one column from it.