[英]Selecting another row based on column value
If I have the query below which selects a forum board, but the board
table contains a column mirror_board_id
. 如果我在下面的查询中选择一个论坛板,但
board
表包含一列mirror_board_id
。 Is it possible for me to modify a basic select query to where if mirror_board_id > 0
that it will SELECT * FROM board WHERE board_id = mirror_board_id
or something. 我是否可以将基本选择查询修改为如果
mirror_board_id > 0
,它将在哪里SELECT * FROM board WHERE board_id = mirror_board_id
或其他内容。 Currently I'm using 2 queries to accomplish this, but that's a bit query intensive... 目前,我正在使用2个查询来完成此操作,但这需要大量查询...
SELECT * FROM board WHERE category_id = 7 AND board_id = 0
update 更新
SELECT * FROM board WHERE category_id = 7 AND
(
(
mirror_board_id > 0
AND board_id = mirror_board_id
)
OR board_id = 0
) AND
display = 1 ORDER BY `order` ASC
This is returning the wrong boards in the list, it's not actually returning the mirror boards, but original boards where mirror_board_id > 0
这将返回列表中的错误板,实际上不是返回镜像板,而是返回
mirror_board_id > 0
原始板
Logically, it is: 从逻辑上讲,它是:
SELECT *
FROM board
WHERE (mirror_board_id > 0 and board_id = mirror_board_id)
or board_id = 0
This can be written more succinctly as: 可以更简洁地写为:
SELECT *
FROM board
WHERE board_id = coalesce(mirror_board_id, 0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.