如何根据 SQL/php 中另一列的多个值从一列中选择单个值
数据库不是我的。结构是
fid| uid | value
3 | 3 | spain
3 | 5 | France
2 | 3 | 45
6 | 3 | male
6 | 5 | female
2 | 5 | 32
字段 ID 是另一个表中的主键,我想忘记。 我试图找到所有具有值“西班牙”、“男性”的 uid
我有以下工作。
SELECT uid
FROM DATABASE
WHERE value IN ('spain', 'male') GROUP BY uid HAVING COUNT(*) >= 2
问题如下..我如何选择男性,西班牙,价值在 20-30 范围内 (这是在西班牙出生、年龄在 20-30 岁之间的男性吗?
非常感谢!!
The database is not mine. The structure is
fid| uid | value
3 | 3 | spain
3 | 5 | France
2 | 3 | 45
6 | 3 | male
6 | 5 | female
2 | 5 | 32
The field ID is primary key in another table, I'd like to forget about.
Im trying to find all uid that have values 'spain', 'male'
I have the following working.
SELECT uid
FROM DATABASE
WHERE value IN ('spain', 'male') GROUP BY uid HAVING COUNT(*) >= 2
The catch is as follows.. How would I select male, spain with value in range of 20-30
(that is males born in spain aged between 20-30?
Thanks a ton!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你的桌子真的很乱。你把不相关的东西的价值混在一起了。
尝试:
请注意,我正在比较字符串和整数。您需要进行测试以了解其效果如何。
另外,我只是把西班牙和男性放在一起,但也许它们真的无关?
fid 决定值的类型吗?
尝试:
这段代码应该工作得更可靠。
You have a really messed up table. You are mixing unrelated things in value.
Try:
Note that I am comparing a string and an integer. You will need to test to see how well that works.
Also, I just put spain and male together, but perhaps they are really unrelated?
Does fid determine the type of value?
Try:
This code should work more reliably.
使用自连接:
Use a self join:
这会起作用。
This will work.
保持语法尽可能接近原来的语法,这可能会起作用:
Keeping your syntax as close to what it was as possible, this might work:
由于您的字段是这样映射的:
您可以将该表转换为更逻辑的视图(在 UID 上索引)。
一旦它位于视图内,您就可以将数据视为常规表。我假设您可能有超过 3 种类型的数据,因为在您的示例中 fid 最多为 6。
假设您已将其放入视图(称为 X)中,您可以执行以下操作:
Since your fields are mapped as so:
You can transform that table into a more logical view (indexed on UID)
Once it is inside a view you can then treat your data as a regular table. I'm assuming you may have more than 3 types of data since fid goes up to 6 on your example.
Assuming you have put that into a view (called X), you can just do: