我有这样的数据:
table 1
|id|fieldname1|fieldname2|price|
table 2
|id|fieldname3|fieldname4|price|
desired result:
|table1_id|table2_id|fieldname1|fieldname2|fieldname3|fieldname4|table1_min_price|table2_min_price|
我基本上可以从表1到表2的左边连接(fieldname1 = fieldname3)从每个表中获得最低价格。
但是,如果表2返回0个查询,则根本不会加入任何结果。 我的意图是如果表2或表1没有结果,它仍然会连接表并创建具有空值的字段。
任何想法如何做到这一点?
答案 0 :(得分:1)
您可能需要一个FULL OUTER JOIN,它将为您提供Table1中的值或Table2中的值或两者。但是,我不认为你可以在它的“无论如何”时获得一排
答案 1 :(得分:1)
将'UNION ALL'更改为'UNION':
SELECT * from
(SELECT origin,destination,min(price) as oneway_cheapest FROM farestable_test where flight_type=0 group by origin,destination )as t1
LEFT JOIN
(SELECT origin,destination,min(price) as return_cheapest FROM farestable_test where flight_type=1 group by origin,destination )as t2
ON (t1.origin = t2.origin AND t1.destination=t2.destination)
UNION
SELECT * from
(SELECT origin,destination,min(price) as oneway_cheapest FROM farestable_test where flight_type=0 group by origin,destination )as t1
RIGHT JOIN
(SELECT origin,destination,min(price) as return_cheapest FROM farestable_test where flight_type=1 group by origin,destination )as t2
ON (t1.origin = t2.origin AND t1.destination=t2.destination)
答案 2 :(得分:0)
您可以完成以下全部外部联接:
select *
from table1 left outer join table2 on(fieldname1=fieldname3)
union
select *
from table1 right outer join table2 on(fieldname1=fieldname3)
答案 3 :(得分:0)
以下是我提出的表格。
CREATE TABLE IF NOT EXISTS `farestable_test` ( `id` tinyint(4) NOT NULL, `origin` varchar(3) NOT NULL, `destination` varchar(3) NOT NULL, `flight_type` tinyint(4) NOT NULL, `price` float NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; SELECT * from (SELECT origin,destination,min(price) as oneway_cheapest FROM farestable_test where flight_type=0 group by origin,destination )as t1 LEFT JOIN (SELECT origin,destination,min(price) as return_cheapest FROM farestable_test where flight_type=1 group by origin,destination )as t2 ON (t1.origin = t2.origin AND t1.destination=t2.destination) UNION ALL SELECT * from (SELECT origin,destination,min(price) as oneway_cheapest FROM farestable_test where flight_type=0 group by origin,destination )as t1 RIGHT JOIN (SELECT origin,destination,min(price) as return_cheapest FROM farestable_test where flight_type=1 group by origin,destination )as t2 ON (t1.origin = t2.origin AND t1.destination=t2.destination)
我想要达到的目标是:
如果数据
INSERT INTO `farestable_test` (`id`, `origin`, `destination`, `flight_type`, `price`) VALUES (1, 'syd', 'mky', 0, 100), (2, 'syd', 'mky', 0, 200),
它显示了正确的结果:
origin destination oneway_cheapest origin destination return_cheapest syd mky 100 null null null
然后......
如果数据
INSERT INTO `farestable_test` (`id`, `origin`, `destination`, `flight_type`, `price`) VALUES (3, 'syd', 'mky', 1, 300), (4, 'syd', 'mky', 1, 400);
它将显示正确的结果:
origin destination oneway_cheapest origin destination return_cheapest null null null syd mky 300
但是,如果数据低于
INSERT INTO `farestable_test` (`id`, `origin`, `destination`, `flight_type`, `price`) VALUES (1, 'syd', 'mky', 0, 100), (2, 'syd', 'mky', 0, 200), (3, 'syd', 'mky', 1, 300), (4, 'syd', 'mky', 1, 400);
这里的结果是错误的:
origin destination oneway_cheapest origin destination return_cheapest syd mky 100 syd mky 300 syd mky 100 syd mky 300
它有重复的数据,这是错误的,我不能完全连接没有与mysql的联合。 还有更好的方法吗?