简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM