Reputation: 17
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?
Upvotes: 0
Views: 999
Reputation: 98
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
Upvotes: 2