0

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?

4
  • 4
    The query seems right. Can you add the DDLs for your table and some sample data? Commented Jul 14, 2014 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. Commented Jul 14, 2014 at 15:49
  • 2
    Are the values NULL? Are the types for the two columns the same. Commented Jul 14, 2014 at 16:05
  • My data type are both integers :/ Commented Jul 15, 2014 at 8:44

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.