Reputation: 11
Ok so I have two mysql tables, the first one is "tbl_forum
":
|MSG_ID|MSG_QUERYID|MSG_ISPARENT| MSG_PARENTID
1 | 59 | 1 | 1
2 | 59 | 0 | 1
The second one is "tbl_frs
":
|FRS_ID|FRS_QUERYID|FRS_PARENTID | FRS_CONTENT
1 | 59 | 1 | xxxx
2 | 59 | 1 | yyyy
I want a query which would yield all rows of both tbl_forum
and tbl_frs
(join) where msg_queryid = 59
and msg_isparent = 1
. So in the example above I would like to get a single row which would look like this:
|MSG_ID|MSG_QUERYID|MSG_ISPARENT| MSG_PARENTID | FRS_ID|FRS_QUERYID|FRS_PARENTID | FRS_CONTENT
|1 |59 | 1 | 1 | 1 | 59 | 1 | xxxx
I tried this:
SELECT * from tbl_frs
JOIN tbl_forum
ON msg_queryid=frs_queryid
WHERE msg_isparent=1
but it yielded two rows... (both msg_id 1 and 2...). How can I fix this?
edit
and the winner is:
SELECT /column names.../ FROM tbl_frs JOIN tbl_forum ON tbl_frs.FRS_PARENTID=tbl_forum.MSG_ID WHERE tbl_frs.FRS_QUERYID=59 AND tbl_forum.MSG_ISPARENT=1
Upvotes: 0
Views: 67
Reputation: 8806
The query you have mentioned in your question has provided the correct answer to your problem.
SELECT * FROM tbl_frs JOIN tbl_forum ON msg_queryid = frs_queryid WHERE msg_isparent = 1
In the above query, you have told MySQL to fetch all rows where MSG_ISPARENT
equals to 1
. So SQL takes the value from the column MSG_QUERYID
(which is 59
) and then it matches the value with tbl_frs.FRS_QUERYID
.
Now there are two rows in FRS_QUERYID
with value 59
. So MySQL will return two rows.
If you add the condition
FRS_ID = 1
it won't meet your requirement. Be careful!!!
Because it won't return "all rows of both tbl_forum
and tbl_frs
(join) where msg_queryid = 59
and msg_isparent = 1
".
The above query gives you the correct results. Don't change it unless you completely understand what's going on.
Upvotes: 0
Reputation: 46259
You should set the condition with FRS_ID=1 AND MSG_ISPARENT =1
then you will get your expect.
You can try this.
SELECT
T1.MSG_ID,
T1.MSG_QUERYID,
T1.MSG_ISPARENT,
T1.MSG_PARENTID,
T2.FRS_ID,
T2.FRS_QUERYID,
T2.FRS_PARENTID,
T2.FRS_CONTENT
FROM tbl_frs AS T2
JOIN tbl_forum AS T1 ON T1.msg_queryid=T2.frs_queryid
WHERE T2.FRS_ID=1 AND T1.MSG_ISPARENT =1
Upvotes: 1
Reputation: 3440
If I understand, you want to select ALL from your two tables where msg_queryid=frs_queryid AND msg_isparent=1
, right?
Btw, using *
is not a good practise, you should name your field !
Try this maybe:
SELECT
FRS.FRS_ID, FRS.FRS_QUERYID, FRS.FRS_PARENTID, FRS.FRS_CONTENT
F.MSG_ID, F.MSG_QUERYID, F.MSG_ISPARENT, F.MSG_PARENTID
FROM tbl_frs as FRS
INNER JOIN tbl_forum as F ON F.MSG_QUERYID=FRS.FRS_QUERYID AND F.MSG_ISPARENT=1
Upvotes: 1
Reputation: 21691
Please try below query:
SELECT * from tbl_forum JOIN tbl_frs ON msg_queryid = frs_queryid WHERE msg_isparent = 1
Thanks!
Upvotes: 0