[英]How to translate this sql to left join without subquery?
我想在mysql中創建視圖,但是在mysql中不支持子查詢。 如何編寫不帶子查詢的sql?
select * from dev_location t1
inner join
(
select
`dev_location`.`device_id` AS `device_id`,
max(`dev_location`.`id`) AS `id`
from
`dev_location`
group by `dev_location`.`device_id`) t2
on t1.id = t2.id
MySQL視圖在from
子句中不支持子查詢。 以下應該在視圖中起作用:
select dl.*
from dev_location dl
where not exists (select 1
from dev_location dl2
where dl2.device_id = dl.device_id and
dl2.id > dl.id
);
這將查詢重新dev_location
為:“從dev_location
中獲取device_id
沒有更大id
所有行。” 這是獲取最大值的尷尬方式。
並且,在dev_location(device_id, id)
上使用索引,它的性能可能比您的版本更好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.