如何根据 SQL/php 中另一列的多个值从一列中选择单个值

发布于 2024-11-25 09:49:25 字数 436 浏览 3 评论 0原文

数据库不是我的。结构是

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 技术交流群。

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

发布评论

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

评论(5

云裳 2024-12-02 09:49:25

你的桌子真的很乱。你把不相关的东西的价值混在一起了。

尝试:

SELECT uid
FROM DATABASE a JOIN
  DATABASE b USING (uid)
WHERE a.value IN ('spain', 'male')
  AND b.value >= 20
  AND b.value <= 30
GROUP BY uid
HAVING COUNT(*) >= 2

请注意,我正在比较字符串和整数。您需要进行测试以了解其效果如何。

另外,我只是把西班牙和男性放在一起,但也许它们真的无关?

fid 决定值的类型吗?

尝试:

SELECT uid
FROM DATABASE country
JOIN DATABASE gender USING (uid)
JOIN DATABASE age USING (uid)
WHERE
  country.fid = 3 AND
  gender.fid = 6 AND
  age.fid = 2 AND
  county.value = 'spain' AND
  gender.value = 'male' AND
  age.value >= 20
  age.value <= 30
GROUP BY uid
HAVING COUNT(*) >= 2

这段代码应该工作得更可靠。

You have a really messed up table. You are mixing unrelated things in value.

Try:

SELECT uid
FROM DATABASE a JOIN
  DATABASE b USING (uid)
WHERE a.value IN ('spain', 'male')
  AND b.value >= 20
  AND b.value <= 30
GROUP BY uid
HAVING COUNT(*) >= 2

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:

SELECT uid
FROM DATABASE country
JOIN DATABASE gender USING (uid)
JOIN DATABASE age USING (uid)
WHERE
  country.fid = 3 AND
  gender.fid = 6 AND
  age.fid = 2 AND
  county.value = 'spain' AND
  gender.value = 'male' AND
  age.value >= 20
  age.value <= 30
GROUP BY uid
HAVING COUNT(*) >= 2

This code should work more reliably.

夜还是长夜 2024-12-02 09:49:25

使用自连接:

SELECT tland.uid
FROM `table` AS tland
INNER JOIN `table` AS tgender ON tland.uid = tgender.uid
INNER JOIN `table` AS tage ON tland.uid = tage.uid
WHERE tland.value = 'spain'
  AND tgender.value = 'male'
  AND 20 <= tage.value AND tage.value <= 30

Use a self join:

SELECT tland.uid
FROM `table` AS tland
INNER JOIN `table` AS tgender ON tland.uid = tgender.uid
INNER JOIN `table` AS tage ON tland.uid = tage.uid
WHERE tland.value = 'spain'
  AND tgender.value = 'male'
  AND 20 <= tage.value AND tage.value <= 30
メ斷腸人バ 2024-12-02 09:49:25

这会起作用。

SELECT d1.uid FROM demo d1
INNER JOIN demo d2 ON d2.uid = d1.uid AND d2.fid = 6 AND d2.value = "Male"
INNER JOIN demo d3 ON d3.uid = d1.uid AND d3.fid = 3 AND d3.value = "Spain"
WHERE (d1.fid = 2 AND (d1.value BETWEEN 20 AND 30))

This will work.

SELECT d1.uid FROM demo d1
INNER JOIN demo d2 ON d2.uid = d1.uid AND d2.fid = 6 AND d2.value = "Male"
INNER JOIN demo d3 ON d3.uid = d1.uid AND d3.fid = 3 AND d3.value = "Spain"
WHERE (d1.fid = 2 AND (d1.value BETWEEN 20 AND 30))
安穩 2024-12-02 09:49:25

保持语法尽可能接近原来的语法,这可能会起作用:

SELECT uid
FROM database
WHERE value in ('spain', 'male') or convert(value, , SIGNED INTEGER) between 30 and 50
GROUP BY uid HAVING COUNT(*) >= 3

Keeping your syntax as close to what it was as possible, this might work:

SELECT uid
FROM database
WHERE value in ('spain', 'male') or convert(value, , SIGNED INTEGER) between 30 and 50
GROUP BY uid HAVING COUNT(*) >= 3
云裳 2024-12-02 09:49:25

由于您的字段是这样映射的:

fid 3 = country
fid 2 = age
fid 6 = sex

您可以将该表转换为更逻辑的视图(在 UID 上索引)。

(SELECT c.uid, c.country, a.age, s.sex FROM
   (SELECT uid, value AS country FROM maintable WHERE fid = 3) c INNER JOIN
   (SELECT uid, value AS age FROM maintable WHERE fid = 2) a ON c.uid = a.uid INNER JOIN
   (SELECT uid, value AS sex FROM maintable WHERE fid = 6) s ON c.uid = s.uid
) as X

一旦它位于视图内,您就可以将数据视为常规表。我假设您可能有超过 3 种类型的数据,因为在您的示例中 fid 最多为 6。

假设您已将其放入视图(称为 X)中,您可以执行以下操作:

SELECT * FROM X
WHERE age BETWEEN 20 AND 30
   AND country = 'spain'
   AND sex = 'male'

Since your fields are mapped as so:

fid 3 = country
fid 2 = age
fid 6 = sex

You can transform that table into a more logical view (indexed on UID)

(SELECT c.uid, c.country, a.age, s.sex FROM
   (SELECT uid, value AS country FROM maintable WHERE fid = 3) c INNER JOIN
   (SELECT uid, value AS age FROM maintable WHERE fid = 2) a ON c.uid = a.uid INNER JOIN
   (SELECT uid, value AS sex FROM maintable WHERE fid = 6) s ON c.uid = s.uid
) as X

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:

SELECT * FROM X
WHERE age BETWEEN 20 AND 30
   AND country = 'spain'
   AND sex = 'male'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文