如果用户有一个值为 1
的 deletion
字段,我如何忽略其他用户的所有评论和主要评论?我的查询会是什么样子。
这是我的 MySQL 表
CREATE TABLE articles_comments (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
parent_comment_id INT UNSIGNED NOT NULL,
user_id INT UNSIGNED NOT NULL,
article_id INT UNSIGNED NOT NULL,
comment TEXT NOT NULL,
date_created DATETIME NOT NULL,
PRIMARY KEY (id),
KEY user_id (user_id),
KEY article_id (article_id)
);
CREATE TABLE users (
user_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
username VARCHAR(255) NULL,
password CHAR(128) NOT NULL,
active CHAR(32),
deletion TINYINT(1) UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (user_id),
UNIQUE KEY (username)
);
最佳答案
假设您的原始查询是
选择---列--- 来自---表--- 哪里---从句---
和 ---tables--- 包括将 root_comment 连接到用户表
例如articles_comments root_comment 加入用户 root_user ON (root_comment.user_id = root_user.user_id)
然后在 ON 子句中添加第二个条件
articles_comments root_comment 加入用户 root_user ON (root_comment.user_id = root_user.user_id and root_user.deletion != 1)
如果这不能满足您的要求,您能澄清一下您的问题吗?
关于MySQL查询设计问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3914075/