Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

Select/Count IF

General discussions related to php

Moderators: macek, egami, gesf

Select/Count IF

Postby lanez89 » Fri Sep 14, 2012 6:21 am

Hello I have the following code:

Code: Select all
$zapytanie1 = "SELECT * FROM `community_challenges` where status=1 and type='bet'";
     $dzialaj1=mysql_query($zapytanie1);

    while ($pole1 = mysql_fetch_array($dzialaj1))
    {
$zapytanie2 = "SELECT * FROM community_matches inner join community_bets on community_matches.datetime between '{$pole1['date_start']}' and '{$pole1['date_finish']}' and community_matches.active=0 and community_bets.matchid=community_matches.id and community_bets.users_id={$pole1['home_id']} and community_bets.status=1 and community_bets.won='y' order by community_bets.id desc limit 10";
     $dzialaj2=mysql_query($zapytanie2);


I need to select only first 10 records between those dates (date_start and date_finish) and I need to count number of rows, where community_bets.won="y" in first 10 records. The following code always = 10 because it selects only won bets. I tried to delete the statement community_bets.won='y' and something like this:
Code: Select all
$i=0;
if ($pole2['won'] == "y") { $i++;}


but it doesn't work. "$zapytanie2" checks more than 1 records, because it's in "while loop"
lanez89
New php-forum User
New php-forum User
 
Posts: 2
Joined: Fri Sep 14, 2012 6:14 am

Re: Select/Count IF

Postby johnj » Sat Sep 15, 2012 7:04 am

the question not clear. if you want only 10 records then you need to use LIMIT in your sql. Then, you need to rewrite your sql according to what you need.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Select/Count IF

Postby lanez89 » Sun Sep 16, 2012 1:09 am

Yes, I know what is LIMIT. But when I limit to first 10 records ( order by community_bets.id asc) i got all types of bets i mean "community_bets.won=y and community_bets.won=n) I want to count only when there is community_bets.won=y. And when I use "where community_bets.won=y order by.." it's wrong, because it gets first 10 records only of won bets ( community_bets.won=y) i want to get the first 10 records with community_bets.won=y and community_bets.won=n and count the number only of " community_bets.won=y"
lanez89
New php-forum User
New php-forum User
 
Posts: 2
Joined: Fri Sep 14, 2012 6:14 am


Return to PHP General

Who is online

Users browsing this forum: No registered users and 4 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.