我有两个表item_info和item_log表如下:
现在我需要搜索'like'item_name,结果将显示任何item_id的最后一行,如:
答案 0 :(得分:1)
SELECT table1
。*,MAX(table1.id)AS last_id,
table2
。table2.id
FROM(table1
)
加入table2
table1
。table2.id
= table2
。table2.id
WHERE item_name
LIKE'%nameOfItem%'
GROUP BY table1
。table2.id
ORDER BY table1
。id
desc
它与我的函数有点相似,我必须为每个线程检索最近的消息。所以我必须加入表并按消息ID分组,并按消息发送的降序排列。
基本上,逻辑是完全相同的。如果它不起作用,请告诉我答案 1 :(得分:1)
解决问题
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
答案 2 :(得分: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 ?
答案 3 :(得分:0)
这可能适合您:
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