We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,328 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Special Select From Where

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
1 Day
Discussion Span
9 Months Ago
Last Updated
5
Views
ckarlss0n
Newbie Poster
4 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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

simplypixie
Practically a Master Poster
642 posts since Oct 2010
Reputation Points: 157
Solved Threads: 118
Skill Endorsements: 5

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

diafol
Keep Smiling
Moderator
10,826 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,532
Skill Endorsements: 61

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.

ckarlss0n
Newbie Poster
4 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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.

diafol
Keep Smiling
Moderator
10,826 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,532
Skill Endorsements: 61

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0722 seconds using 2.73MB