You can get this information by making a join
of the table with itself:
SELECT
t2.*
FROM
josyo_rsform_submission_values t1
INNER JOIN josyo_rsform_submission_values t2
ON (t1.id = t2.id)
WHERE
t1.FieldName LIKE :term
Construction of the query
Okay, let's explain little by little.
We want to get all tuples that have a id
. For this, we need to get id
somehow, but the basic information we have is FieldName
.
We can get the id
of the row of this table that contains this information as you did above:
SELECT
id
FROM
josyo_rsform_submission_values
WHERE
FieldName LIKE :term
Okay, now we need to get all the rows in a table that match this information. If I treat as different tables, t1
and t2
, I need all the information of t2
that join t1
through id
. This way of thinking indicates that we can use join
; just adding the information of a t1
and t2
any would be like this:
SELECT
*
FROM
t1
INNER JOIN t2
ON (t1.id = t2.id)
If we only want information from t2
:
SELECT
t2.*
FROM
t1
INNER JOIN t2
ON (t1.id = t2.id)
Using the correct name for the tables:
SELECT
t2.*
FROM
josyo_rsform_submission_values t1
INNER JOIN josyo_rsform_submission_values t2
ON (t1.id = t2.id)
Filtering by the desired information in t1
:
SELECT
t2.*
FROM
josyo_rsform_submission_values t1
INNER JOIN josyo_rsform_submission_values t2
ON (t1.id = t2.id)
WHERE
t1.FieldName LIKE :term
UPDATE : It was not very clear in the first version of the answer how a join of a table works on itself
Explaining self-join
Let's take a set of data. Let's say it is the X
set. For ease of understanding, my dataset belongs to . I'll also say that the first field of this data is called id
, and the second field is called value
. My data set X
is:
(1, 12)
(1, 100)
(2, 15)
(2, 37)
(2, 0)
I can get all elements in X
that have id = 1
. The notation at relational algebra looks something like:
Sotheresultofthisalgebraicexpressionis:
(1,12)(1,100)
So,whatwouldbetheresultofthefollowingexpression?
Inparts:
ontheleftside,wehavetheselectionfrombeforeGiventhisselection,Imakearenameofid
toid1
;thismeansthattheresultofthisoperationwillnowhavecolumnid1
andcolumnvalue
naturaljoinoperationoftheleftset(wejustdefineditinthepreviousstep)withtherightset,yettobedefined;thejoinisdoneusingid1=id2
fromX
datasetIrenamefromid
toid2
;notethatinnoneoftheoperationsuntilnowhasthevalueofthedatasetX
changedDisplayingtheresultsofeachoperation:
Ontheleftside,wehavetheselectionfrombefore
[id,value](1,12)(1,100)
Giventhisselection,Imakearenameofid
toid1
;thismeansthattheresultofthisoperationwillnowhavecolumnid1
andcolumnvalue
[id1,value](1,12)(1,100)
ofdatasetX
Imaketherenameofid
toid2
;notethatinnoneoftheoperationssofarhasthevalueofthedatasetX
changed
[id2,value](1,12)(1,100)(2,15)(2,37)(2,0)
Aboutstep3...AjoinismadeupofaCartesianproductfollowedbyaselection,soIwilldivideitintotwosteps3(3.aand3.b)
a. with the right set
[id1, value, id2, value]
(1, 12, 1, 12)
(1, 12, 1, 100)
(1, 12, 2, 15)
(1, 12, 2, 37)
(1, 12, 2, 0)
(1, 100, 1, 12)
(1, 100, 1, 100)
(1, 100, 2, 15)
(1, 100, 2, 37)
(1, 100, 2, 0)
b. the join is done using id1 = id2
[id1, value, id2, value]
(1, 12, 1, 12)
(1, 12, 1, 100)
(1, 100, 1, 12)
(1, 100, 1, 100)
Now imagine that you no longer want to filter by id = 1
, but by value = 100
. Thus, we would have the following expression:
The result of this operation is:
[id1, value, id2, value]
(1, 100, 1, 12)
(1, 100, 1, 100)
And that's more or less what you wanted in the beginning. There are some changes when you switch from relational algebra to SQL (for example, the% rename operation is done in the data set, not in the column), but the general idea is this.