我正在尝试使用slim框架从mysql数据库中检索特定的数据集。我的SQL查询没有给我严格的要求。任何帮助将不胜感激!
查询:
function getModules($matric)
{
$sql = "SELECT DISTINCT students.module1,time_tables.days, time_tables.time FROM `students`,`time_tables`
WHERE students.matricNos = '$matric' AND students.module1 = time_tables.module_tag
UNION
SELECT DISTINCT students.module2,time_tables.days, time_tables.time FROM `students`,`time_tables`
WHERE students.matricNos = '$matric' AND students.module2 = time_tables.module_tag
UNION
SELECT DISTINCT students.module3,time_tables.days, time_tables.time FROM `students`,`time_tables`
WHERE students.matricNos = '$matric' AND students.module3 = time_tables.module_tag";
try
{
$db = getConnection();
$stmt = $db-> query($sql);
$result = $stmt-> fetchAll(PDO::FETCH_OBJ);
$db=null;
echo json_encode($result);
}
catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
我从查询中得到的内容
[{"module1":"ACC07103","days":"Thursday","time":"09:00-10:00"},{"module1":"CLP07112","days":"Tuesday","time":"14:00-15:00"},{"module1":"BMS08100","days":"Thursday","time":"10:00-11:00"}]
这里发生的是它给了我正确的值,但它给了我相同的标签,如module1,当它显示另一个模块时,它应该是module2,但它也显示模块1,模块3也是如此
我想要的结果;
[{"module1":"ACC07103","days":"Thursday","time":"09:00-10:00"},{"module2":"CLP07112","days":"Tuesday","time":"14:00-15:00"},{"module3":"BMS08100","days":"Thursday","time":"10:00-11:00"}]
在我的数据库中,列被标记为module1,module2,module3,但我不明白为什么它给了我正确的值,但不同列的名称相同..
谢谢..
答案 0 :(得分:1)
我怀疑这与UNION从第一个SELECT查询中获取列名有关。
如果您将SELECT DISTINCT students.module1
更改为SELECT DISTINCT students.module1 as module
,
答案 1 :(得分:0)
试试:
$sql = "SELECT students.module1,null as module2,null as module3, time_tables.days, time_tables.time FROM `students`,`time_tables`
WHERE students.matricNos = '$matric' AND students.module1 = time_tables.module_tag
GROUP BY students.module1
UNION
SELECT null as module1,students.module2,null as module3, time_tables.days, time_tables.time FROM `students`,`time_tables`
WHERE students.matricNos = '$matric' AND students.module2 = time_tables.module_tag
GROUP BY students.module2
UNION
SELECT null as module1, null as module2,students.module3,time_tables.days, time_tables.time FROM `students`,`time_tables`
WHERE students.matricNos = '$matric' AND students.module3 = time_tables.module_tag
GROUP BY students.module3";
在这个方法中,如果不匹配,你将选择模块1,模块2,模块3为空值。
答案 2 :(得分:0)
除非您想要运行3个查询,否则无法完全按照您的要求进行操作 您的列名称无法在查询中更改。
您可以运行3个查询,而不是在SQL中使用UNION。
然后将您的结果加在一起,例如:
$result1 = $stmt1-> fetchAll(PDO::FETCH_OBJ);
$result2 = $stmt2-> fetchAll(PDO::FETCH_OBJ);
$result3 = $stmt3-> fetchAll(PDO::FETCH_OBJ);
$result = array_merge($result1,$result2,$result3);
或者,您可以在查询中添加一个额外的列来指示模块:
$sql = "
SELECT DISTINCT 'module1' as module, students.module1,time_tables.days, time_tables.time
FROM `students`,`time_tables`
WHERE students.matricNos = '$matric' AND students.module1 = time_tables.module_tag
UNION
SELECT DISTINCT 'module2',students.module2,time_tables.days, time_tables.time
FROM `students`,`time_tables`
WHERE students.matricNos = '$matric' AND students.module2 = time_tables.module_tag
UNION
SELECT DISTINCT 'module3', students.module3,time_tables.days, time_tables.time
FROM `students`,`time_tables`
WHERE students.matricNos = '$matric' AND students.module3 = time_tables.module_tag";
然后,您必须构建显示或保存例程以从其他列中提取模块名称,而不是使用列名本身。