3
select c.kupon, count(*) as count
            from kuponbahis c
            join bahis b 
                on b.sonuc = c.secim 
                and b.ID = c.bahis 
            group by c.kupon
            having count(case when c.bahis ='$sonuclandirilacakbahis' then 1 end) > 0

With this query I'm getting kupon IDs and counts. Then fetching them in PHP and matching with the result of

SELECT COUNT(*) FROM kuponbahis WHERE kupon='$kuponid'

(while fetching the second query, too.) If it's a match, i'm doing some work.

But now, I want to do this in SQL directly.

My PHP code is below;

$kazananKuponlariGetirSorgu = mysql_query("select c.kupon, count(*) as count
from kuponbahis c
join bahis b 
    on b.sonuc = c.secim 
    and b.ID = c.bahis 
group by c.kupon
having count(case when c.bahis ='$sonuclandirilacakbahis' then 1 end) > 0");

while ($kazananKuponlariGetirSorgux = mysql_fetch_array($kazananKuponlariGetirSorgu)){

        $kuponid = $kazananKuponlariGetirSorgux[0];
        $tutanbahisadet = $kazananKuponlariGetirSorgux[1];


        $kupondakiBahisAdeti = mysql_query("SELECT COUNT(*) FROM kuponbahis WHERE kupon='$kuponid'");
        $kupondakiBahisAdetix = mysql_fetch_array($kupondakiBahisAdeti);

        if ($kupondakiBahisAdetix[0]==$tutanbahisadet){
            //it's a match
        }

I tried many queries but they all failed. How can I merge this two sql process to only one?

match

2
  • 1
    Could you maybe show us a little of your data, or the structure of the table? Commented Jan 10, 2016 at 14:42
  • php.net/manual/function.mysql-query.php ... Be aware of mysql_query is dead .. Commented Jan 10, 2016 at 14:50

1 Answer 1

4

The obvious way is in the having clause:

select c.kupon, count(*) as count
from kuponbahis c join
     bahis b 
     on b.sonuc = c.secim and b.ID = c.bahis 
group by c.kupon
having sum(c.bahis = '$sonuclandirilacakbahis') > 0 and
       count(*) = (SELECT COUNT(*) FROM kuponbahis WHERE kupon = '$kuponid');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.