#1242-子查询返回多于1行-MySQL [英] #1242 - Subquery returns more than 1 row - mysql

查看:209
本文介绍了#1242-子查询返回多于1行-MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做出一条选择语句,从MySQL数据库中选择图像名称. 该表称为-pictures_archive.我也试图根据它们的类别来选择这些图片.代码是:

I am trying to make a select statement that selects the image names from a MySQL database. The table is called - pictures_archive. I am also trying to select these pictures depending on the category they have. The code is:

SELECT pictures_archive_filename FROM pictures_archive 
WHERE pictures_archive_id = (SELECT pictures_archive_id 
FROM pictures_archive_category WHERE pictures_category_id = 9)

它给我一个#1242-子查询返回多于1行"的错误.我知道为什么,但是不知道怎么做.

It gives me an "#1242 - Subquery returns more than 1 row" error. I can see why, but can't figure it out how to do it.

推荐答案

由于子查询可以返回多个值,因此IN应该适合您的where子句.

Since your subquery can return multiple values, IN should fit in youy where clause.

SELECT pictures_archive_filename 
FROM pictures_archive 
WHERE pictures_archive_id IN 
(
   SELECT pictures_archive_id 
   FROM pictures_archive_category 
   WHERE pictures_category_id = 9
)

对此的另一种选择是join这两个表,效率更高.

an alternative for this is to join both tables which is more efficient.

SELECT  pictures_archive_filename 
FROM    pictures_archive a 
        INNER JOIN pictures_archive_category b
            ON a.pictures_archive_id = b.pictures_archive_id
WHERE   b.pictures_category_id = 9

这篇关于#1242-子查询返回多于1行-MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆