[英]mysql error : “subquery returns more than one row” but i need more than one row from subquery
I have two tables in my problem, one is the USER
table with all details of user and a REQUEST
table with friend requests from all users to all users. 我的问题中有两个表,一个是
USER
表,其中包含USER
所有详细信息,另一个是REQUEST
表,其中包含从所有用户到所有用户的好友请求。 I want to list all the friends of one user who logs into my application. 我想列出一个登录我的应用程序的用户的所有朋友。
The USER
table attributes are, USER
表的属性是
id | uname | passwd | fname | e mail | dob | mobileno | habbits | tastes | profession | image
11 | nagaraj | ***** | naga | ng@gml | 3/94 | 998345 | singing | sports | teacher
12 | chiraag | ******* | chiru | ch@gml | 2/93 | 894617 | writing | music | student
Similarly, the REQUEST table attributes are, 同样,REQUEST表属性是
rqstTO | rqstFM | fname | e mail | mobile | status
chirag | nagaraj | nagaraj | ng@gml | 99821 | accepted
The tables look like this. 表格看起来像这样。 Now I want to display the details of the friends( from
USER
table), only those friends who have accepted the request from the user who logged in. Output may be multiple users. 现在,我想显示好友的详细信息(来自
USER
表),仅显示那些已接受登录用户的请求的好友。输出可能是多个用户。 The conditions to be satisfied are 要满足的条件是
all details of friends (from user table) 朋友的所有详细信息(来自用户表)
rqstFM
should be the user logged in and rqstTO
must be the friends to whom he sent requests (since request table has request details from all to all users) rqstFM
应该是登录的用户, rqstTO
必须是他向其发送请求的朋友(因为请求表包含了所有用户的请求详细信息)
status=accepted
(they must have accepted the request) status=accepted
(他们必须已接受请求) The query I have written is 我写的查询是
SELECT *
FROM user
WHERE habits='singing' AND uname = (
SELECT rqstTO
FROM request
WHERE rqstFM ='"+username+"' AND status='Accepted'
);
(here +username+
is the uname of the person logged in obtained from HTML FORM tag). (此处
+username+
是从HTML FORM标记获得的登录+username+
)。 The error I keep getting is "subquery returns more than one row" but I do need multiple rows as logged in user may have sent requests to many ppl and among those all who accepted his request, their details need to be displayed from user table. 我一直收到的错误是“子查询返回多个行”,但是我确实需要多行,因为登录的用户可能已将请求发送到许多ppl,并且在所有接受他的请求的人中,需要从用户表中显示其详细信息。 Please, correct my query or provide an appropriate query for my problem
请更正我的查询或为我的问题提供适当的查询
You need to use IN
instead of =
: 您需要使用
IN
而不是=
:
You can use as mentioned below: 您可以按如下所述使用:
SELECT * FROM user
WHERE habits='singing' AND uname IN
( SELECT rqstTO FROM request WHERE rqstFM ='"+username+"' AND status='Accepted');
However using subqueries are not recommended due to performance issues. 但是,由于性能问题,不建议使用子查询。 You should rewrite your query by using
JOIN
您应该使用
JOIN
重写查询
You can use 'JOIN' like below: (I haven`t tested it) 您可以使用“ JOIN”,如下所示:(我尚未测试过)
SELECT u.* FROM user u
JOIN request r ON u.uname = r.rqstTO
WHERE u.habits='singing' AND r.rqstFM ='"+username+"' AND r.status='Accepted';
You want something like this (not tested it however): 您想要这样的东西(但是未经测试):
SELECT user.uname, request.fname
FROM user
INNER JOIN request ON request.fname = user.fname
WHERE request.status = 'Accepted';
However, I would probably have a bit of a rethink of those tables, maybe this is personal preference but you would likely be better off having a join table that simply contains an id and the username, and then join on two other tables. 但是,我可能会对这些表进行一些重新思考,也许这是个人喜好,但是最好只包含一个包含ID和用户名的联接表,然后再联接另外两个表。 One containing the requests and one containing the member details.
一种包含请求,另一种包含成员详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.