获取两个不同表的结果mysql [英] obtain results of two different tables mysql
本文介绍了获取两个不同表的结果mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两张桌子.
餐桌存货
fecha | tarifa
2017-01-06 500
2017-01-07 500
2017-01-08 500
餐桌促销
fecha | percent
2017-01-07 0.10
2017-01-08 0.10
2017-01-07 0.15
2017-01-08 0.15
我需要在mysql查询时获取:
and i need to obtain while mysql query this:
fecha | tarifa | percent
2017-01-06 500 0
2017-01-07 500 0.10
2017-01-08 500 0.10
2017-01-06 500 0
2017-01-07 500 0.15
2017-01-08 500 0.15
谢谢!!!
更新
我需要用查询结果创建这个数组.
i need that create this array with the result of the query.
Array
(
[1] => Array
(
[0] => Array
(
[fecha] => 2017-01-06
[tstandard]=>500
[porcentaje] => 0.00
)
[1] => Array
(
[fecha] => 2017-01-07
[tstandard]=>500
[porcentaje] => 0.10
)
[2] => Array
(
[fecha] => 2017-01-08
[tstandard]=>500
[porcentaje] => 0.10
)
)
[2] => Array
(
[0] => Array
(
[fecha] => 2017-01-06
[tstandard]=>500
[porcentaje] => 0.00
)
[1] => Array
(
[fecha] => 2017-01-07
[tstandard]=>500
[porcentaje] => 0.15
)
[2] => Array
(
[fecha] => 2017-01-08
[tstandard]=>500
[porcentaje] => 0.15
)
)
)
我已经完成了数组但没有每天的标准
I have done the array but without the tstandard of each day
推荐答案
您可以使用 left outer join
来获得所需的输出,例如:
You can use left outer join
to get the desired output, e.g.:
SELECT s.fecha, s.tarifa, IFNULL(p.percent, 0)
FROM stock s LEFT JOIN promotions p ON s.fecha = p.fecha;
这是 SQL Fiddle.
更新
如果你想用日期限制输出,你可以使用下面的查询:
If you want to restrict the output with dates, you can use the below query:
SELECT s.fecha, s.tarifa, IFNULL(p.percent, 0)
FROM stock s LEFT JOIN promotions p ON s.fecha = p.fecha
WHERE s.fecha BETWEEN $checkin AND $checkout;
这篇关于获取两个不同表的结果mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文