Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $uid = 7;
- ## Простой count одиночный
- $auth = $pdo->prepare('SELECT COUNT(*) FROM `user`');
- $auth->bindParam(':u_id', $uid);
- $auth->execute();
- $Count = $auth->fetchAll();
- $Num = implode('', $Count[0]);
- echo "Num = $Num<br>";
- ## UNION
- $a1 = microtime(true);
- $auth = $pdo->prepare('SELECT COUNT(*) as t1_count, NULL FROM `organization` t1 WHERE u_id=:u_id
- UNION
- SELECT NULL, COUNT(*) as t2_count FROM `event_org` WHERE u_id=:u_id ');
- $auth->bindParam(':u_id', $uid);
- $auth->execute();
- $Count2 = $auth->fetchAll();
- $a2 = microtime(true);
- echo "<pre> Count2 ";
- var_dump($Count2);
- echo "</pre>===========================================";
- ## inner join
- $a3 = microtime(true);
- $auth = $pdo->prepare('SELECT
- COUNT(*) as t1_count,
- t2.t2_count
- FROM `organization` t1
- inner join
- (
- SELECT u_id, COUNT(*) as t2_count FROM `event_org` WHERE u_id=:u_id
- ) t2 on t2.u_id = t1.u_id
- WHERE t1.u_id = :u_id ');
- $auth->bindParam(':u_id', $uid);
- $auth->execute();
- $Count3 = $auth->fetchAll();
- $a4 = microtime(true);
- echo "<pre> Count2 ";
- var_dump($Count3);
- echo "</pre>===========================================";
- ## Результаты
- $b1 = $a2-$a1; $b2 = $a4-$a3;
- echo "Первый $b1 Второй $b2";
- ## Вид результатов
- ==== UNION =======================================
- array(2) {
- [0]=>
- array(2) {
- ["t1_count"]=>
- string(1) "1"
- ["NULL"]=>
- NULL
- }
- [1]=>
- array(2) {
- ["t1_count"]=>
- NULL
- ["NULL"]=>
- string(1) "1"
- }
- }
- ==== JOIN =======================================
- array(1) {
- [0]=>
- array(2) {
- ["t1_count"]=>
- string(1) "1"
- ["t2_count"]=>
- string(1) "1"
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement