Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Jak kilka SELECT połączyć w 1 zapytanie sql
zielq701
post
Post #1





Grupa: Zarejestrowani
Postów: 8
Pomógł: 0
Dołączył: 8.07.2013

Ostrzeżenie: (0%)
-----


Witam wszystkich,
piszę proste api do planera dnia. Czy da się zastąpić 3 SELECTY jednym aby otrzymać następującą odpowiedź:

  1. (
  2. [2015-05-22] => Array
  3. (
  4. [timed] => Array
  5. (
  6. [0] => Array
  7. (
  8. [id] => 16
  9. [user_id] => 1
  10. [task] => Testowe zadanie specjalne
  11. [note] => Testowo testować testowane zadania. Test testowy testowany testowo wielokrotnie.
  12. [priority_id] => 1
  13. [date] => 2015-05-22
  14. [time] => 00:00:00
  15. [done] => 0
  16. [timestamp] => 2015-05-22 09:59:34
  17. )
  18.  
  19. )
  20.  
  21. [other] => Array
  22. (
  23. [0] => Array
  24. (
  25. [id] => 14
  26. [user_id] => 1
  27. [task] => Testowe zadanie specjalne
  28. [note] => Testowo testować testowane zadania. Test testowy testowany testowo wielokrotnie.
  29. [priority_id] => 1
  30. [date] => 2015-05-22
  31. [time] =>
  32. [done] => 0
  33. [timestamp] => 2015-05-22 09:59:20
  34. )
  35.  
  36. )
  37.  
  38. [done] => Array
  39. (
  40. )
  41.  
  42. )
  43.  
  44. [2015-05-23] => Array
  45. (
  46. [timed] => Array
  47. (
  48. [0] => Array
  49. (
  50. [id] => 15
  51. [user_id] => 1
  52. [task] => Testowe zadanie specjalne
  53. [note] => Testowo testować testowane zadania. Test testowy testowany testowo wielokrotnie.
  54. [priority_id] => 1
  55. [date] => 2015-05-23
  56. [time] => 00:00:00
  57. [done] => 0
  58. [timestamp] => 2015-05-22 09:59:23
  59. )
  60.  
  61. )
  62.  
  63. [other] => Array
  64. (
  65. )
  66.  
  67. [done] => Array
  68. (
  69. )
  70.  
  71. )
  72.  
  73. )


Bardzo mi zależy na takiej strukturze tabeli. Tak wygląda teraz część mojego kodu w php:

  1. class DataProvider {
  2. private $db;
  3.  
  4. public function __construct($db)
  5. {
  6. $this->db = $db;
  7. }
  8.  
  9. public function getRequestData()
  10. {
  11. return json_decode(file_get_contents('php://input'), true);
  12. }
  13.  
  14. public function getTasks($userId, $date)
  15. {
  16. //pobiera zadania ktore maja okreslona godzine i NIE zostaly wykonane
  17. $sql = "SELECT ds_tasks.*
  18. FROM ds_tasks LEFT JOIN ds_priorities
  19. ON ds_tasks.priority_id=ds_priorities.id
  20. WHERE user_id = :uid AND `date` = :date AND done = 0 AND`time` IS NOT NULL";
  21.  
  22. $timed = $this->db->fetchAll($sql, array( 'uid' => (int) $userId, 'date' => $date));
  23.  
  24. //pobiera zadania ktore NIE maja okreslonej godziny i NIE zostaly wykonane
  25. $sql = "SELECT ds_tasks.*
  26. FROM ds_tasks LEFT JOIN ds_priorities
  27. ON ds_tasks.priority_id=ds_priorities.id
  28. WHERE user_id = :uid AND `date` = :date AND done = 0 AND `time` IS NULL";
  29.  
  30. $other = $this->db->fetchAll($sql, array( 'uid' => (int) $userId, 'date' => $date));
  31.  
  32. //pobiera zadania ktore sa wykonane
  33. $sql = "SELECT ds_tasks.*
  34. FROM ds_tasks LEFT JOIN ds_priorities
  35. ON ds_tasks.priority_id=ds_priorities.id
  36. WHERE user_id = :uid AND `date` = :date AND done = 1 AND `time` IS NULL";
  37.  
  38. $done = $this->db->fetchAll($sql, array( 'uid' => (int) $userId, 'date' => $date));
  39.  
  40. //tworzy tabele z zadaniami
  41. $tasks = array (
  42. 'timed' => $timed,
  43. 'other' => $other,
  44. 'done' => $done
  45. );
  46. return $tasks;
  47. }
  48.  
  49. public function getTimeIntervalTasks($userId, $date, $endDate)
  50. {
  51. $tasks = array();
  52.  
  53. while (strtotime($date) <= strtotime($endDate)) {
  54.  
  55. $tasks[$date] = $this->getTasks($userId, $date);
  56.  
  57. $date = date("Y-m-d", strtotime("+1 day", strtotime($date)));
  58. }
  59.  
  60. return $tasks;
  61. }
  62. }


Z góry dzięki za jakąkolwiek pomoc (IMG:style_emoticons/default/tongue.gif) .
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 24.08.2025 - 09:43