RobMor
RobMor

Reputation: 57

MYSQL query with values of the same table

I'm developing a PHP application for querying a MySQL DB. My first query asks user to choice a value, that has a correspondence with the entry Sbj_ID in my table called 'Rec_SW2_Rel'. The value is correctly returned by the PHP function.

Now I have to query the table once again and perform following selection: imagine that the already chosen Sbj_ID is '9', I must return all the values of all those relations for which Rec_ID is equal and Position is = '2'.

Table 'Rec_SW2_Rel' looks like:

+ ---------------------------- +
* Rec_ID | Sbj_ID | Position | *
+ ---------------------------- +
*   10   |    9   |     1    | *
*   10   |  165   |     2    | *
*   10   |   23   |     3    | *
*   11   |    9   |     1    | *
*   11   |   15   |     2    | *
*   12   |   64   |     1    | *
*   12   |    8   |     2    | *
+ ---------------------------- +

Expected output should be:

10 | 165 | 2

11 | 15 | 2

Upvotes: 0

Views: 39

Answers (1)

fancyPants
fancyPants

Reputation: 51938

select
*
from
your_table
where Position = 2 
and Rec_ID in (select Rec_ID from your_table where Sbj_ID = 9)

Upvotes: 1

Related Questions