计算表中出现的记录是其中之一,而不是其他:MYSQL

发布于 2024-10-19 23:33:07 字数 784 浏览 2 评论 0原文

我有两个简单的表

users

+----+--------+-----------+
| id | gender | birthdate |
+----+--------+-----------+

userpreference

+----+------------------+-----------------+
| id | preference value | preference type |
+----+------------------+-----------------+

问题:

我想查询所有未列出特定偏好值(例如“购物”)的人。这包括所有列出了特定偏好值的人。也没有列出任何偏好类型,因此该列可能为空,但是由于用户偏好的列“id”将用户引用为外键,我还想将所有未出现在第二个表中的人(用户偏爱)?

没有偏好值“购物”作为偏好值的总人数:

这是我尝试过的:

SELECT
(
SELECT COUNT(DISTINCT userpreference.id) FROM userpreference
WHERE  preferencevalue != 'shopping')
+
(
SELECT COUNT(users.id)
FROM users
WHERE users.id NOT IN
(SELECT userpreference.Id
FROM userpreference )
)
AS'Total'

I have a two simple tables

users

+----+--------+-----------+
| id | gender | birthdate |
+----+--------+-----------+

userpreference

+----+------------------+-----------------+
| id | preference value | preference type |
+----+------------------+-----------------+

Question:

I want to query all people who have not listed a specific preference value such as 'shopping'.This includes all people who have not listed any preference types as well so that column could be null, however since userpreference's column 'id' references users as a foreign key, I also want to include in my count all people who don't show up in the second table (user preference)?

Total # of people who do not have preference value 'shopping' as their preference value:

Here is what i have tried:

SELECT
(
SELECT COUNT(DISTINCT userpreference.id) FROM userpreference
WHERE  preferencevalue != 'shopping')
+
(
SELECT COUNT(users.id)
FROM users
WHERE users.id NOT IN
(SELECT userpreference.Id
FROM userpreference )
)
AS'Total'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

醉南桥 2024-10-26 23:33:07
Select Count(*)
From Users
Where Not Exists   (
                    Select 1
                    From UserPreference As UP1
                    Where UP1.id = Users.id
                        And UP1.PreferenceValue = 'Shopping'
                    )
Select Count(*)
From Users
Where Not Exists   (
                    Select 1
                    From UserPreference As UP1
                    Where UP1.id = Users.id
                        And UP1.PreferenceValue = 'Shopping'
                    )
木緿 2024-10-26 23:33:07

尝试 RIGHT JOIN,这将包括所有未出现在第二个表中的人

SELECT *
来自用户
RIGHT JOIN 用户首选项 ON ( users.userID = Users.userID)
WHERE首选项值='购物'

Try a RIGHT JOIN, that will include all people who dont show up in the second table

SELECT *
FROM Users
RIGHT JOIN Userpreference ON ( users.userID = Users.userID)
WHERE preference_value = 'shopping'

遗失的美好 2024-10-26 23:33:07

试试这个:

SELECT COUNT(DISTINT U.id) FROM users U NATURAL LEFT JOIN userpreference UP
WHERE UP.preferencevalue IS NULL OR UP.preferenceValue != '购物';

LEFT JOIN 应该引入所有用户记录,无论他们是否有 UP 记录。

Try this:

SELECT COUNT(DISTINT U.id) FROM users U NATURAL LEFT JOIN userpreference UP
WHERE UP.preferencevalue IS NULL OR UP.preferenceValue != 'shopping';

The LEFT JOIN should bring in all the users records whether or not they have a UP record.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文