0

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

3
Contributors
4
Replies
5
Views
4 Years
Discussion Span
Last Post by diafol
0

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

0

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.

0

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.

Edited by diafol

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.