简体   繁体   中英

subquery with an inner join

I'm stuck on a subquery issue, trying to get multiple columns while using a join.

I'm trying to grab different fields from other tables, based on what the "type" field is set as, for a notifications system that tracks different sections of a site.

I was hoping I could do something like this:

SELECT
    n.`id`,
    n.`last_date`,
    IF(n.type = 'liked', (SELECT co.`article_id`, a.article_id FROM `articles_comments` co INNER JOIN `articles` a ON a.article_id = co.article_id WHERE co.`comment_id` = n.data_id), NULL),
    n.`comment_id`,
            n.`type`,
            n.`data_id`,
            FROM
                `user_notifications` n
            WHERE n.`owner_id` = 8505

The problem, is that phpmyadmin gives me an error of "#1241 - Operand should contain 1 column(s)"

I'm pretty confused, because if I do no INNER JOIN and only grab the "article_id" it works fine.

Sub-query must return only one column

you can see this link

this query

(SELECT  a.article_id 
FROM `article
s_comments` co 
INNER JOIN `articles` a 
ON a.article_id = co.article_id 
WHERE co.`comment_id` = n.data_id)

must be something like this

(SELECT  a.article_id 
FROM `article
s_comments` co 
INNER JOIN `articles` a 
ON a.article_id = co.article_id 
WHERE co.`comment_id` = n.data_id)
UNION ALL
(SELECT co.`article_id`
FROM `article
s_comments` co 
INNER JOIN `articles` a 
ON a.article_id = co.article_id 
WHERE co.`comment_id` = n.data_id)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM