gpt4 book ai didi

php - MySQL从php中的查询结果进行查询

转载 作者:行者123 更新时间:2023-11-29 08:42:08 25 4
gpt4 key购买 nike

如果我想在php中查询一个查询结果该怎么办?假设我有这个..

$result = mysql_query("SELECT * FROM tbl_x WHERE tbl_x.attribute = y");

现在我需要查询 $result 以过滤从其查询中获得的结果。请注意,我不想这样做...

$result2 = mysql_query("SELECT * FROM ( SELECT * FROM tbl_x WHERE tbl_x.attribute = y ) AS tbl_x1 WHERE tbl_x1... etc");

我想避免这种情况的原因是在执行这样的查询时出现“重复列”错误。

我正在寻找类似的东西

$result = mysql_query("SELECT * FROM tbl_x");
$result2 = mysql_query_result($result);

最佳答案

您可以在搜索条件中应用多个术语,并用 AND 分隔:

$result = mysql_query("SELECT * FROM tbl_x 
WHERE tbl_x.attribute = y
AND tbl_x.attribute2 = z");
<小时/>

回复您上面的评论。听起来您正在使用 Entity-Attribute-Value设计。

为了匹配多个属性,你必须采取一些技巧。通常,WHERE 子句一次只能应用于一行。但由于每个属性都存储在单独的行中,因此您需要执行以下两种解决方案之一:

  1. 将多行合并为一行,以便您可以在一个条件下对所有属性使用 WHERE。

    SELECT config_id
    FROM attributes AS s
    JOIN attributes AS c USING (config_id)
    JOIN attributes AS l USING (config_id)
    WHERE (s.attr, s.value) = ('size', 'M')
    AND (c.attr, c.value) = ('colour, 'green')
    AND (l.attr, l.value) = ('cloth', 'cotton);
  2. 搜索多个属性中的任意一个,然后如果匹配的行数等于您要搜索的属性数,则您已找到全部属性。

    SELECT config_id
    FROM attributes
    WHERE (attr, value) = ('size', 'M')
    OR (attr, value) = ('colour', 'green')
    OR (attr, value) = ('cloth', 'cotton')
    GROUP BY config_id
    HAVING COUNT(DISTINCT attr) = 3;

关于php - MySQL从php中的查询结果进行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13520596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com