Jump to content

PHP/MYSQL: Compare two columns in a table to see if they are equal


beckjoh

Recommended Posts

Sorry I accidently hit return before I finished typing this post.

 

I am trying to figure out how to query my table to see if two columns are storing identical values.  For example:  I have a mysql table like this:

 

id    column1  column2

1        3.65        3.65

2        4.10        4.10

3        5.23        5.23

 

I want to query the table to compare column1 and column2 to see is every row is equal or if any is not equal.  They must either be all equal or if one row is not equal then the query would return false.  Any help would be greatly appreciated.

 

JB

How about...

$true_flag = true;

 

$query = "SELECT * FROM table";

$result = mysql_query($query) or die(mysql_error());

while ($row = mysql_fetch_object($result)) {

if($row->column1 != $row->column2){

        $true_flag = false;

        } 

}

if($true_flag){

//do whatever

}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.