1

I have two table item_info and item_log tables are given below:

Item_info

Item_log

Now i need to search by 'like' item_name and the result will show last row of any item_id like:

Result

5
  • Try a inner join on those two tables. Commented Aug 14, 2014 at 6:11
  • en.wikipedia.org/wiki/Join_(SQL) Commented Aug 14, 2014 at 6:12
  • 1
    item_log.item_id field is not looking unique, then how to decide which rows will come from item_log table. Commented Aug 14, 2014 at 6:15
  • What do YOU want the result to contain? Joining the tables will get you results, but the logic to filter that result set has to come from your side. Commented Aug 14, 2014 at 6:18
  • i need last row of any item_id @Girish Commented Aug 14, 2014 at 6:29

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

last_id correct but other information not match@user07192010
You can just modify it depending on your desired outputs
1

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

Comments

0
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 ?

Comments

0

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  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.