Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a table with several columns and I am trying to select the values that are equal in 2 columns.

I'm creating a simple select

select * from table_name where column1 = column_2

but it doesn't return nothing.

Am I doing right? Of course not unless it will work :) However, for example if I use the operator

!=

it returns the correct rows. What is happening?

share|improve this question
4  
The query seems right. Can you add the DDLs for your table and some sample data? –  Mureinik Jul 14 '14 at 15:49
2  
Show you table definition and some data. Are the cases the same in both rows? Is there white space in one? Lots of reasons why col1 = col2 is not returning any rows. –  Bob Jul 14 '14 at 15:49
2  
Are the values NULL? Are the types for the two columns the same. –  Gordon Linoff Jul 14 '14 at 16:05
    
My data type are both integers :/ –  user3279149 Jul 15 '14 at 8:44

1 Answer 1

I'm not reproducing your issue, which tells me you may have made a mistake when creating your table? I don't think the query is the issue.

Here's my example, step by step. First, we create the table (I'm using PostgreSQL 9.1):

CREATE TABLE example(
id int,
name1 text,
name2 text
);

Then I populate it with these rows:

INSERT INTO example (id, name1, name2)
VALUES (0, 'Bird', 'Cheese');
INSERT INTO example (id, name1, name2)
VALUES (1, 'Bear', 'Honey');
INSERT INTO example (id, name1, name2)
VALUES (2, 'Fish', 'Fish');
INSERT INTO example (id, name1, name2)
VALUES (3, 'Bread', 'Bread');

Okay, so now, when I use a query with your format, I should get the rows with id = 2 and id = 3:

SELECT * FROM example WHERE name1 = name2;

Which returned the correct result:

 id | name1 | name2
----+-------+-------
  2 | Fish  | Fish
  3 | Bread | Bread

I suggest you try this example to see if it works with your version of PostgreSQL. If it does, fantastic! That means there is probably just mistake in your table implementation somewhere (perhaps mismatched data types?). If the example does not work, perhaps there is something wrong with your PostgreSQL version

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.