我有table_1
和table_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
答案 0 :(得分:2)
如果我的问题是对的,你可以使用这样的联盟 -
select * from table_1 union select * from table_2 order by create_date desc
修改强>
创建一个这样的视图 -
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
答案 1 :(得分:1)
另外我看到@Rehban的答案对你来说很好,如果你不需要view
,我会提出另一个解决方案:
SELECT
mainTable.id,
mainTable.created,
mainTable.qty,
mainTable.comments,
mainTable.last_update,
table_7.firstname,
SUBSTRING(table_7.lastname, 1, 1) AS lastname,
table_8.title country,
table_3.value AS sim,
mainTable.offer_request,
table_5.manufacturer AS manufacturer,
table_4.name AS model,
table_6.value AS specifications,
table_9.value as shipping,
mainTable.model AS mid,
mainTable.user_id,
mainTable.callme,
mainTable.phoneprice,
mainTable.phoneprice_eur,
mainTable.currency,
mainTable.sel_buy_show_all,
mainTable.seller_buyer
FROM (Select * From `table_1` union Select * From `table_2`) as mainTable
LEFT JOIN `table_3` ON `table_3`.`id` = `mainTable `.`sim`
LEFT JOIN `table_4` ON `table_4`.`id` = `mainTable `.`model`
LEFT JOIN `table_5` ON `table_5`.`id` = `mainTable `.`manufacturer`
LEFT JOIN `table_6` ON `table_6`.`id` = `mainTable `.`specifications`
LEFT JOIN `table_7` ON `table_7`.`id` = `mainTable `.`user_id`
LEFT JOIN `table_8` ON `table_7`.`country`=`table_8`.`id`
LEFT JOIN `table_9` ON `table_9`.`id` = `mainTable `.`types`
WHERE `mainTable `.`status` = '1'
AND `mainTable `.`deleted` = '0'
ORDER BY `last_update` DESC
LIMIT 200