sponturious
sponturious

Reputation: 131

Not getting desired result back from SQL

I am trying to retrieve specific set of data from mysql database using slim framework. My sql query is not giving me exacting what i want. 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

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..

Thanks..

Upvotes: 0

Views: 67

Answers (3)

Loopo
Loopo

Reputation: 2195

Unless you want to run 3 queries, there is no way to do exactly what you want.
Your column name can't change mid-query.

You could run 3 queries rather than use UNION in the SQL.

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.

Upvotes: 0

echo_Me
echo_Me

Reputation: 37233

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.

Upvotes: 0

Philip Derbyshire
Philip Derbyshire

Reputation: 61

I suspect this is to do with the UNION taking the column names from the first SELECT query.

Does this still happen if you change SELECT DISTINCT students.module1 to SELECT DISTINCT students.module1 as module?

Upvotes: 1

Related Questions