[英]SQL Select query join multiple tables
I have this code: 我有以下代码:
function getPricing() {
$returnarr = array();
$dcid = (isset($_REQUEST['dcid'])) ? "WHERE datacentre_id='" . mysql_real_escape_string($_REQUEST['dcid']) . "'" : '';
$addons = $_REQUEST['addon'];
if ($addons == "true")
$sql = "SELECT * FROM addonprices $dcid ORDER BY addon_desc ASC";
else
$sql = "SELECT l.l_name, p.* FROM pricing p LEFT JOIN locations l ON l.location_id=p.datacentre_id $dcid ORDER BY CASE splitsize WHEN 'f' THEN 0 WHEN 'w' THEN 1 WHEN 'h' THEN 2 WHEN 'q' THEN 3 WHEN '2U' THEN 4 WHEN '1U' THEN 5 ELSE 6 END DESC, amps ASC";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
$returnarr[] = $row;
}
header('Content-type: application/json');
echo json_encode($returnarr);
}
I need to add another table into both select statements, joining on the $dcid
, the table is called slas
and the column is currency
I've tried a few things but no luck. 我需要在两个select语句中添加另一个表,并在
$dcid
上进行$dcid
,该表称为slas
而该列是currency
我已经尝试了几件事但没有运气。
I'm trying to pull the currency into a javascript file to get prices using this switch statement: 我正在尝试使用以下switch语句将货币提取到javascript文件中以获取价格:
var currency = "";
switch (row.currency) {
case '1' :
currency = "£";
break;
case '2' :
currency = " $";
break;
case '3' :
currency = "€";
break;
default:
currency = "£";
break;
}
and even when currency should equal 2 or 3 it still shows up as the default... I'm not sure if it's the switch statement that's wrong or the sql query. 甚至当Currency等于2或3时,它仍然会显示为默认值...我不确定是switch语句出错还是sql查询。
UPDATE 更新
Just having issues with this one line: 仅有这一行的问题:
$sql = "SELECT l.l_name, p.*, slas.currency FROM pricing p LEFT JOIN locations l ON l.location_id=p.datacentre_id LEFT JOIN slas ON slas.datacentre_id = p.datacentre_id $dcid ORDER BY CASE splitsize WHEN 'f' THEN 0 WHEN 'w' THEN 1 WHEN 'h' THEN 2 WHEN 'q' THEN 3 WHEN '2U' THEN 4 WHEN '1U' THEN 5 ELSE 6 END DESC, amps ASC";
Taken from an answer below. 取自以下答案。
I think the issue here is that the unqualified datacentre_id
in your WHERE
will not allow you to join to another table with a column named datacentre_id
我认为这里的问题是
WHERE
不合格的datacentre_id
不允许您使用名为datacentre_id
的列连接到另一个表
I would suggest the WHERE be 我建议在哪里
$dcid = (isset($_REQUEST['dcid'])) ? "WHERE p.datacentre_id='" . mysql_real_escape_string($_REQUEST['dcid']) . "'" : '';
and the query be 和查询是
if ($addons == "true")
$sql = "SELECT p.*, slas.currency FROM addonprices p LEFT JOIN slas ON slas.datacentre_id = p.datacentre_id $dcid ORDER BY addon_desc ASC";
else
$sql = "SELECT l.l_name, p.*, slas.currency FROM pricing p LEFT JOIN locations l ON l.location_id=p.datacentre_id LEFT JOIN slas ON slas.datacentre_id = p.datacentre_id $dcid ORDER BY CASE splitsize WHEN 'f' THEN 0 WHEN 'w' THEN 1 WHEN 'h' THEN 2 WHEN 'q' THEN 3 WHEN '2U' THEN 4 WHEN '1U' THEN 5 ELSE 6 END DESC, amps ASC";
notice in both cases the table with datacentre_id
in it is aliased p 请注意,在两种情况下,其中具有
datacentre_id
的表都使用别名p
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.