简体   繁体   English

无法从SQL返回期望的结果

[英]Not getting desired result back from SQL

I am trying to retrieve specific set of data from mysql database using slim framework. 我正在尝试使用苗条的框架从mysql数据库检索特定的数据集。 My sql query is not giving me exacting what i want. 我的SQL查询没有给我确切的我想要的。 any help would be much appreciated! 任何帮助将非常感激!

Query : 查询:

    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() .'}}';
    }

}

what I am getting back from the query 我从查询中得到什么

[{"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"}]

whats happening here is its giving me the correct values but its giving me the same tags like module1 and when it shows another module it should be module2 but it shows module 1 as well and same for module 3 这里发生的是它给了我正确的值,但是给了我相同的标签,例如module1,当它显示另一个模块时,它应该是module2,但它也显示了模块1,并且对于模块3也是相同的

my desired result; 我想要的结果;

[{"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"}]

in my database the columns are labeled like module1, module2, module3 but i don't understand why it is giving me the correct values but same names for different columns.. 在我的数据库中,这些列被标记为module1,module2,module3,但我不明白为什么它给我正确的值,但不同列的名称相同。

Thanks.. 谢谢..

I suspect this is to do with the UNION taking the column names from the first SELECT query. 我怀疑这与UNION从第一个SELECT查询中获取列名有关。

Does this still happen if you change SELECT DISTINCT students.module1 to SELECT DISTINCT students.module1 as module ? 如果将SELECT DISTINCT students.module1更改为SELECT DISTINCT students.module1 as module是否还会发生这种情况?

try that: 尝试:

 $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";

in this aproach , you will select module1 , module2, module3 with null value if it doesnt match. 在此方法中,您将选择null值不匹配的module1,module2,module3。

Unless you want to run 3 queries, there is no way to do exactly what you want. 除非您要运行3个查询,否则无法完全执行所需的操作。
Your column name can't change mid-query. 您的列名不能在查询中更改。

You could run 3 queries rather than use UNION in the SQL. 您可以运行3个查询,而不是在SQL中使用UNION。

Then join your results together, something like: 然后将结果汇总在一起,例如:

 $result1 = $stmt1-> fetchAll(PDO::FETCH_OBJ);
 $result2 = $stmt2-> fetchAll(PDO::FETCH_OBJ);
 $result3 = $stmt3-> fetchAll(PDO::FETCH_OBJ);

 $result = array_merge($result1,$result2,$result3);

Alternatively, you can add an extra column in your query to indicate the module: 或者,您可以在查询中添加额外的列以指示模块:

$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";

Then you will have to construct your display or saving routine to pull the module name from a different column, rather than using the column name itself. 然后,您将必须构造显示或保存例程,以从其他列中提取模块名称,而不是使用列名称本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM