Advertisement
zero50x

UNION и JOIN

Dec 14th, 2015
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.76 KB | None | 0 0
  1. $uid = 7;
  2.  
  3. ## Простой count одиночный
  4. $auth = $pdo->prepare('SELECT COUNT(*) FROM `user`');
  5. $auth->bindParam(':u_id', $uid);
  6. $auth->execute();
  7. $Count = $auth->fetchAll();
  8. $Num = implode('', $Count[0]);
  9. echo "Num = $Num<br>";
  10.  
  11. ## UNION
  12. $a1 = microtime(true);
  13.     $auth = $pdo->prepare('SELECT COUNT(*) as t1_count, NULL FROM `organization` t1 WHERE u_id=:u_id
  14.    UNION
  15.    SELECT NULL, COUNT(*) as t2_count FROM `event_org` WHERE u_id=:u_id ');
  16.     $auth->bindParam(':u_id', $uid);
  17.     $auth->execute();
  18.     $Count2 = $auth->fetchAll();
  19. $a2 = microtime(true);
  20.  
  21. echo "<pre> Count2 ";
  22. var_dump($Count2);
  23. echo "</pre>===========================================";
  24.  
  25. ## inner join
  26. $a3 = microtime(true);
  27.     $auth = $pdo->prepare('SELECT
  28.    COUNT(*) as t1_count,
  29.    t2.t2_count
  30.    FROM `organization` t1
  31.    inner join
  32.    (
  33.    SELECT u_id, COUNT(*) as t2_count FROM `event_org` WHERE u_id=:u_id
  34.    ) t2 on t2.u_id = t1.u_id
  35.    WHERE t1.u_id = :u_id ');
  36.    
  37.    
  38.     $auth->bindParam(':u_id', $uid);
  39.     $auth->execute();
  40.     $Count3 = $auth->fetchAll();
  41. $a4 = microtime(true);
  42.  
  43. echo "<pre> Count2 ";
  44. var_dump($Count3);
  45. echo "</pre>===========================================";
  46.  
  47. ## Результаты
  48. $b1 = $a2-$a1; $b2 = $a4-$a3;
  49. echo "Первый $b1 Второй $b2";
  50.  
  51. ## Вид результатов
  52. ==== UNION =======================================
  53. array(2) {
  54.   [0]=>
  55.   array(2) {
  56.     ["t1_count"]=>
  57.     string(1) "1"
  58.     ["NULL"]=>
  59.     NULL
  60.   }
  61.   [1]=>
  62.   array(2) {
  63.     ["t1_count"]=>
  64.     NULL
  65.     ["NULL"]=>
  66.     string(1) "1"
  67.   }
  68. }
  69. ==== JOIN =======================================
  70. array(1) {
  71.   [0]=>
  72.   array(2) {
  73.     ["t1_count"]=>
  74.     string(1) "1"
  75.     ["t2_count"]=>
  76.     string(1) "1"
  77.   }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement