[英]Get data and sort from MySQL two tables
I have table_1
and table_2
and they are the same structure, but different data. 我有table_1
和table_2
,它们的结构相同,但是数据不同。 Need to get all the data from that tables in sort of create_date
and no matter from which table (it can be 1 row from table_1, 3 rows from table_2 and then again from table_1). 需要以create_date
形式从该表中获取所有数据,而不管从哪个表中获取(它可以是来自table_1的1行,来自table_2的3行,然后又来自table_1的行)。 Is it possible? 可能吗? How? 怎么样? or shout I get two queries and then sort by date with PHP? 或喊我得到两个查询,然后用PHP按日期排序?
EDIT: Sorry for first part, I thought I can do it from there, but seems like i can't :/ 编辑:对不起,第一部分,我以为我可以从那里开始,但是好像我不能:/
i have very big query like this 我有这样很大的查询
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
And there is table_1 which structure is the same as table_2, and I need somehow to insert table_2 to the query with all joins like table_1 还有table_1的结构与table_2相同,并且我需要以某种方式将table_2插入到具有所有联接的查询中,例如table_1
If I got your question right, you can use union like this - 如果我的问题正确,那么您可以使用工会-
select * from table_1 union select * from table_2 order by create_date desc
EDIT 编辑
Create a view like this - 创建这样的视图-
create view table_1And2 as select * from table_1 union select * from table_2
table_1And2 is not a good name, give a meaningful name. table_1And2不是一个好名字,给一个有意义的名字。
And modify your long query like this - 然后像这样修改您的长查询-
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
Also I see the answer of @Rehban is a good one for you, I will produce another solution if you do not need view
: 我也看到@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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.