gpt4 book ai didi

PHP/PDO奇怪的请求

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

我想做一个 PDO 请求,从 2 个不同的表获取信息,但事情很快就会变得困难,我解释一下:

我有第一个数据库表(花),其组织方式如下(但它比我向您展示的要大):

ID;       name;              price of format 1; price of format 2; name of other format 3; price of other format 3;
FLOW0001; big yellow flower; ; 15,99; more big format; 34,99;
FLOW0002; little red flower; 5,99; 8,99; ; ;

...就这样继续下去...

第二个表(树)是这样的:

ID;       name;            name of format;  price of format;
TREE0001; OMG BIG TREE !; F*** big format; 599,99;
TREE0001; OMG BIG TREE !; F*** even bigger format; 899,99;
TREE0002; litte ugly tree; little format; 20,99;

...就这样继续下去...

问题是我想“合并”这两个表并将它们显示在一个页面中混合在一起,如下所示:

while ($datas =$response->fetch()) //fetching the tables, they will be togheter and ordered by (their biggest) price. So, trees and flowers will blend. 
{
// then echo the fetched datas. I will need to separate the prices and show them... some if/else based on pregmatches of the ID to know if it's an TREE or FLOW.
}

如何混合这个?我没有任何想法。最糟糕的是我需要做一个页面系统(每页 50 个)。在页面系统和页面中,所有重复的树都需要仅显示为唯一树...帮助!

最佳答案

您可以使用union从两个表中获取数据。只需确保您在两个查询中返回匹配的列即可。因此,如果在第一个查询中具有以下数据类型 int、text、varchar、decimal、decimal,您将需要确保第二个查询获取相同的列类型,否则数据库将出现 hissyfit关于它。您可以在末尾使用 order by 子句来很好地对两个查询中的数据进行排序。

像这样的事情应该可以解决问题:

select
ID,
name,
price
from
flowers
where
// your clauses
union
select
ID,
name,
price
from
trees
where
// your clauses
order by 3 desc

编辑:如果需要,您可以在其中使用 group_concat() 字段,是的,您将能够在 PHP 中对它们执行 explode() 操作。默认的 group_concat 分隔符是 , (逗号),因此如果您的字段中有逗号,则需要将其更改为某些内容,以便爆炸正常工作。

关于PHP/PDO奇怪的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11750322/

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