gpt4 book ai didi

php - 从MySQL两张表中获取数据并排序

转载 作者:太空宇宙 更新时间:2023-11-03 11:48:50 26 4
gpt4 key购买 nike

我有table_1table_2,它们是相同的结构,但数据不同。需要以 create_date 的形式从该表中获取所有数据,无论来自哪个表(它可以是来自 table_1 的 1 行,来自 table_2 的 3 行,然后再次来自 table_1)。是否可以?如何?或者喊我得到两个查询,然后用 PHP 按日期排序?

编辑:对于第一部分,我感到很抱歉,我以为我可以从那里做到,但似乎我做不到:/

我有这样一个非常大的查询

SELECT 
table_1.id,
table_1.created,
table_1.qty,
table_1.comments,
table_1.last_update,
table_7.firstname,
SUBSTRING(table_7.lastname, 1, 1) AS lastname,
table_8.title country,
table_3.value AS sim,
table_1.offer_request,
table_5.manufacturer AS manufacturer,
table_4.name AS model,
table_6.value AS specifications,
table_9.value as shipping,
table_1.model AS mid,
table_1.user_id,
table_1.callme,
table_1.phoneprice,
table_1.phoneprice_eur,
table_1.currency,
table_1.sel_buy_show_all,
table_1.seller_buyer
FROM (`table_1`)

LEFT JOIN `table_3` ON `table_3`.`id` = `table_1`.`sim`
LEFT JOIN `table_4` ON `table_4`.`id` = `table_1`.`model`
LEFT JOIN `table_5` ON `table_5`.`id` = `table_1`.`manufacturer`
LEFT JOIN `table_6` ON `table_6`.`id` = `table_1`.`specifications`
LEFT JOIN `table_7` ON `table_7`.`id` = `table_1`.`user_id`
LEFT JOIN `table_8` ON `table_7`.`country`=`table_8`.`id`
LEFT JOIN `table_9` ON `table_9`.`id` = `table_1`.`types`
WHERE `table_1`.`status` = '1'
AND `table_1`.`deleted` = '0'
ORDER BY `last_update` DESC
LIMIT 200

还有 table_1,它的结构与 table_2 相同,我需要以某种方式将 table_2 插入到查询中,所有连接都像 table_1

最佳答案

如果我答对了你的问题,你可以像这样使用 union -

select * from table_1 union select * from table_2 order by create_date desc

编辑

像这样创建一个 View -

create view table_1And2 as select * from table_1 union select * from table_2

table_1And2这个名字不好,起个有意义的名字。

然后像这样修改您的长查询 -

SELECT 
table_1And2.id,
table_1And2.created,
table_1And2.qty,
table_1And2.comments,
table_1And2.last_update,
table_7.firstname,
SUBSTRING(table_7.lastname, 1, 1) AS lastname,
table_8.title country,
table_3.value AS sim,
table_1And2.offer_request,
table_5.manufacturer AS manufacturer,
table_4.name AS model,
table_6.value AS specifications,
table_9.value as shipping,
table_1And2.model AS mid,
table_1And2.user_id,
table_1And2.callme,
table_1And2.phoneprice,
table_1And2.phoneprice_eur,
table_1And2.currency,
table_1And2.sel_buy_show_all,
table_1And2.seller_buyer
FROM (`table_1And2`)

LEFT JOIN `table_3` ON `table_3`.`id` = `table_1And2`.`sim`
LEFT JOIN `table_4` ON `table_4`.`id` = `table_1And2`.`model`
LEFT JOIN `table_5` ON `table_5`.`id` = `table_1And2`.`manufacturer`
LEFT JOIN `table_6` ON `table_6`.`id` = `table_1And2`.`specifications`
LEFT JOIN `table_7` ON `table_7`.`id` = `table_1And2`.`user_id`
LEFT JOIN `table_8` ON `table_7`.`country`=`table_8`.`id`
LEFT JOIN `table_9` ON `table_9`.`id` = `table_1And2`.`types`
WHERE `table_1And2`.`status` = '1'
AND `table_1And2`.`deleted` = '0'
ORDER BY `last_update` DESC
LIMIT 200

关于php - 从MySQL两张表中获取数据并排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36819794/

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