How to use field from subquery in MySQL?

自古美人都是妖i 提交于 2019-12-12 06:39:05

问题


I've got the following query:

SELECT insent.id, notifications.id
FROM insent
WHERE insent.id IN (
    SELECT insent_id
    FROM notifications
)
;

But this gives an error saying:

Unknown column 'notifications.id' in 'field list'

Any idea how I can do this?


回答1:


If you want to get data from few tables in one query then you should use JOIN construction. For example:

SELECT 
    insent.id, 
    notifications.id 
FROM insent 
JOIN notifications ON notifications.insent_id = insent.id

if you don't want it you must remove notifications.id field from field list

SELECT 
   insent.id
FROM insent
WHERE insent.id IN (
    SELECT insent_id FROM notifications
)



回答2:


The problem here is notifications table is not in the scope Try like this

SELECT 
   i.id, 
   n.id 
FROM insent AS i
JOIN notifications AS n ON n.insent_id = insent.id


来源:https://stackoverflow.com/questions/53501040/how-to-use-field-from-subquery-in-mysql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!