Forum Moderators: coopster

Message Too Old, No Replies

getting total rows on 2 values

need total rows when 2 statements are true

         

weddingm

1:36 pm on Jun 17, 2009 (gmt 0)

10+ Year Member



I am trying to get the total rows in the table where quote and response are both not nr. However, I need it to pull a row when response=NR and quotable=1. It's not doing that with the below code.

//THIS CALCULATES THE TOTAL RATING GIVEN WHEN ALL CATEGORIES ARE NOT NR RESPONSE.

$totalraterows=mysql_query("SELECT * FROM table WHERE ((response !='NR' AND quotable !='NR') AND (vendorusername='$webinfo2'))");
$total9=0;
while ($totalrows = mysql_fetch_array($totalraterows)) {
$total9=mysql_affected_rows();}

jatar_k

3:18 pm on Jun 17, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what happens when you execute the query? do you get errors?

you could use count

SELECT count(columnname) as mybigtotal FROM table WHERE ((response !='NR' AND quotable !='NR') AND (vendorusername='$webinfo2')

if you need to get a row afterwards that has different values then I would issue a different query

mooger35

3:29 pm on Jun 17, 2009 (gmt 0)

10+ Year Member



If memory serves mysql_affected_rows won't return anything for a SELECT query.

Try mysql_num_rows [us.php.net]

//THIS CALCULATES THE TOTAL RATING GIVEN WHEN ALL CATEGORIES ARE NOT NR RESPONSE.
$totalraterows=mysql_query("SELECT * FROM table WHERE ((response !='NR' AND quotable !='NR') AND (vendorusername='$webinfo2'))");

$total9=0;
$total9=mysql_num_rows($totalraterows); // you don't need the while loop

coopster

9:42 pm on Jun 17, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If memory serves mysql_affected_rows won't return anything for a SELECT query.

True. The relative mysqli function [php.net] works a little differently though.

weddingm

2:19 am on Jun 18, 2009 (gmt 0)

10+ Year Member



The issue is that
(response !='NR' AND quotable !='NR') 
is true when on or the other is not NR

I want it so that is is only true when BOTH columns are NR.

weddingm

3:39 am on Jun 18, 2009 (gmt 0)

10+ Year Member



got it to work as below:

((response !='NR') ¦¦ (quotable !='NR'))

coopster

11:17 am on Jun 18, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Your wording was the confusing part of this thread, as it is contradictory:

>>where quote and response are both not nr. (first message)
>>only true when BOTH columns are NR. (last message)
>>I need it to pull a row when response=NR and quotable=1 (last message)

... but it is becoming clear now. You are actually checking the value of just this part of the WHERE condition expression, not the entire SQL statement.

If you want a TRUE response returned for this portion of the conditional expression when either the response OR quotable value is not equal to 'NR' then you have your condition correct now. Except for one thing, change those pipes to the SQL boolean "OR" operator because pipe symbols in standard sql statements typically translate to concatenation, especially for database servers running in ANSI mode, which is closer to standard SQL.

MySQL Server understands the ¦¦ and && operators to mean logical OR and AND, as in the C programming language. In MySQL Server, ¦¦ and OR are synonyms, as are && and AND. Because of this nice syntax, MySQL Server doesn't support the standard SQL ¦¦ operator for string concatenation; use CONCAT() instead.

MySQL Extensions to Standard SQL [dev.mysql.com]
PIPES_AS_CONCAT [dev.mysql.com]
Running MySQL in ANSI Mode [dev.mysql.com]

weddingm

10:16 pm on Jun 19, 2009 (gmt 0)

10+ Year Member



I really want to pull data from the database when ROW 1: field A is not NR at the same time field B is not NR. The AND in between did not get the desired result I thought it would. The OR I feel would be situation a or b and would not get the desired result.

So confused. I hope this helps the communication problem.

coopster

12:07 pm on Jun 20, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I really want to pull data from the database when ROW 1: field A is not NR at the same time field B is not NR. The AND in between did not get the desired result I thought it would.

Then you are wording your plain text phrase here incorrectly because your original code says exactly that:

(response !='NR' AND quotable !='NR')
That code says to return TRUE when ROW 1:

field A [response] is not [!=] NR AND ["at the same time"] field B [quotable] is not [!=] NR.

coopster

12:29 pm on Jun 20, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I forgot to add, if you want it to return TRUE when either one OR the other condition is true, you use OR rather than AND -- and I confirmed you on this in your last message, I just recommend using the OR operator as opposed to the double pipes as they can mean concatenation.
(response != 'NR' OR quotable != 'NR')
That code says to return TRUE when ROW 1:

field A [response] is not [!=] NR OR ["either this is TRUE OR the next is TRUE"] field B [quotable] is not [!=] NR.