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

Link to comment
Share on other sites

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

}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.