Hello everyone.
I need some help with a SELECT FROM WHERE query.

Let's say I have a row named A and another row named B, and I want to select the eventual cases where values from these two rows, somewhere, match eachother.
In this case, you can see, that the only value that you can find in the both rows is '1'. (Check the table below).
How can I make a SELECT statement out of this, where I want to sort out these occasions on when there are values of the same in the two rows?

SELECT * FROM table WHERE a = b would not find anything, because as you can see in my example table below there are no occasions where a = b.
However, both row A and B have got a value of '1' somewhere, and THIS is what I want my query to find.
The '1' in row A is in the first line, and the '1' in row B is in the second line. Hope you understand what I'm asking for.
Thanks in advance!

Example Table:

A B
1 0
2 1
3 6
4 8

What I want as the result:

A B
1 1

Use a join on the same table, something like:

SELECT tbl1.*, tbl2.* FROM table_name AS tbl1 INNER JOIN table_name AS tbl2 ON tbl1.a=tble2.b

Not tested and a quick example

Member Avatar for diafol

When you say 'row' - do you mean 'column' (field)?

Hmm. Interesting simplypixie, I'll look it up! Is what you wrote maybe somewhat similar to the UNION ALL command?

@diafol Whoops. Of course I do, my bad! Nice catch.

Member Avatar for diafol

Most simple queries will give something like this:

A B
1 0
2 1

If you use an UNION or an OR operator in the WHERE clause. BUt you say that you want a single row:

A B
1 1

Perhaps something like this - but there may be an easier way:

(SELECT A, B FROM AB WHERE A = 1 LIMIT 1) UNION (SELECT A,B FROM AB WHERE B = 1 LIMIT 1)

If mysql_num_rows == 2 then you've got a match. It won't give the query result as a single row though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.