php - Checking certain row from a table which is referenced to another table? -
i know question not make sense @ asking, mean that..... make more clear, illustrate problem in examples:
i have userpost table contains posts different users.
userpost table:
+---------+--------+--------------+ | postid | userid | postmessage | +---------+--------+--------------+ | 1 | 3 | sometext | | 2 | 5 | sometext | | 3 | 2 | sometext | | 5 | 6 | sometext | +---------+--------+--------------+ (!remember that userid is being referenced to users table)
i have table called favorites postid being referenced userpost table:
favorites table:
+---------+--------+ | postid | userid | +---------+--------+ | 1 | 5 | | 3 | 2 | +---------+--------+ what want grab data userpost , check if userpost has been favorited (where userid = 5) let's say
i tried using query that's not want!
select *, (select exists( select * `favorites` inner join `userpost` on favourites.postid = userpost.postid favorites.postid = 1 , favorites.userid = 5)) isfavourited userpost; this result of following query:
+---------+--------+-------------+--------------+ | postid | userid | postmessage | isfavourited | +---------+--------+-------------+--------------+ | 1 | 3 | sometext | 1 | | 2 | 5 | sometext | 1 | | 3 | 2 | sometext | 1 | | 5 | 3 | sometext | 1 | +---------+--------+-------------+--------------+ i know making mistake inside query using:
(where favorites.postid = 1 , favorites.userid = 5)
which return true.
i give example of what want:
lets (userid = 5) wants grab userpost , must result below:
+---------+--------+-------------+--------------+ | postid | userid | postmessage | isfavourited | +---------+--------+-------------+--------------+ | 1 | 3 | sometext | 1 | | 2 | 5 | sometext | 0 | | 3 | 2 | sometext | 0 | | 5 | 3 | sometext | 0 | +---------+--------+-------------+--------------+
try:
select *, postid in (select postid favourites userid = 5) isfavourited userpost your query checks row exists user 5 favourites post 1; not user 5 favorites post being selected in row of return.
Comments
Post a Comment