[英]issue using mysql with union all
select `C1`.`ID` AS `ID`,
`C1`.`Article_ID` AS `Article_ID`,
`C1`.`DateAdded` AS `DateAdded`,
`C1`.`Votes` AS `Votes`,
`C1`.`Content` AS `Content`,
(select 0) AS `isReply`,
`C1`.`Usr_ID` AS `Usr_ID`,
(select NULL) AS `RepliedTo`
from `Comment` as C1
where isnull(`C1`.`Reply_ID`) limit 5
UNION ALL
select
`C2`.`Reply_ID` AS `Reply_ID`,
`C2`.`Article_ID` AS `Article_ID`,
`C2`.`DateAdded` AS `DateAdded`,
`C2`.`Votes` AS `Votes`,
`C2`.`Content` AS `Content`,
`C2`.`ID` AS `isReply`,
`C2`.`Usr_ID` AS `Usr_ID`,
`C2`.`RepliedTo` AS `RepliedTo`
from `Comment` as C2
where (`C2`.`Reply_ID` = `C1`.`Reply_ID` )
My issue is in the last line where (C2.Reply_ID = C1.Reply_ID )
in specific the C2.Reply_ID=C1.Reply_ID
我的问题是在最后一行
where (C2.Reply_ID = C1.Reply_ID )
具体是C2.Reply_ID=C1.Reply_ID
an Error with Unknown column C1.Reply_ID in where clause
is popping out. 弹出
Unknown column C1.Reply_ID in where clause
的错误。
My Objective is that I want to Retrieve 我的目标是我要检索
All rows of Table1 表1的所有行
Union All 联合所有
The rows of Table2 using the ID's retrieved from Table1 in where clause 使用where子句中的Table1检索到的ID的Table2行
You can not use the tables from the first select in the second select. 您不能使用第二个选择中的第一个选择中的表。
You have to repeat your first query in the second select query 您必须在第二个选择查询中重复第一个查询
select `C1`.`ID` AS `ID`,
`C1`.`Article_ID` AS `Article_ID`,
`C1`.`DateAdded` AS `DateAdded`,
`C1`.`Votes` AS `Votes`,
`C1`.`Content` AS `Content`,
(select 0) AS `isReply`,
`C1`.`Usr_ID` AS `Usr_ID`,
(select NULL) AS `RepliedTo`
from `Comment` as C1
where isnull(`C1`.`Reply_ID`) limit 5
UNION ALL
select
`C2`.`Reply_ID` AS `Reply_ID`,
`C2`.`Article_ID` AS `Article_ID`,
`C2`.`DateAdded` AS `DateAdded`,
`C2`.`Votes` AS `Votes`,
`C2`.`Content` AS `Content`,
`C2`.`ID` AS `isReply`,
`C2`.`Usr_ID` AS `Usr_ID`,
`C2`.`RepliedTo` AS `RepliedTo`
from `Comment` as C2
where (`C2`.`Reply_ID` in (select `C1`.`ID`
from `Comment` as C1
where isnull(`C1`.`Reply_ID`) limit 5)
This should give you close to what you want: 这应该使您接近所需的内容:
SELECT `ID` AS `ID`
, `Article_ID` AS `Article_ID`
, `DateAdded` AS `DateAdded`
, `Votes` AS `Votes`
, `Content` AS `Content`
, IF(copyTbl.copyNum=1, 0, `ID`) AS `isReply`
, `Usr_ID` AS `Usr_ID`
, IF(copyTbl.copyNum=1, NULL, `RepliedTo`) AS `RepliedTo`
FROM `Comment`, (SELECT 1 AS copyNum UNION SELECT 2) AS copyTbl
WHERE `Reply_ID` IS NULL
ORDER BY `ID`, copyTbl.copyNum
LIMIT 10
;
It assumes Comment
. 它假定为
Comment
。 ID
is unique; ID
是唯一的; if that is not the case, you'll need to adjust the ORDER BY
accordingly. 如果不是这种情况,则需要相应地调整
ORDER BY
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.