SELECT `tag`
FROM `f_tags`
INNER JOIN `f_tag_f_user`
ON `f_tag_f_user`.`tag_id` = `f_tags`.`tag_id`
WHERE `f_tag_f_user`.`user_id` = '$_SESSION[user_id]'
will this type of join work in the following situation as well
I have three tables (many-to-many) - (USER) (USER_TAG_LINK) and (TAG)
- a user enters a tag to be INSERTed in the db
- before the tag is put in the db a check is done to see if the tag is already in the TAG table
- if it is - another check is done to check if the tag is linked to the current user in the (USER_TAG_LINK)
------- if the tag already exists for the user an error message is displayed
------- if the tag does not belong to the user a link is inserted into USER_TAG_LINK
- if the tag doen't exist it is INSERTed
I am in the middle of trying to do this in a similar way to the first code example i submitted.
Would it be better (or possible) to use a join to do this or would it be better to split these into two different queries -
one to check if the current user has already submitted the tag and then a separate one to see if any other user has submitted that tag?
Bit of a long one, sorry, but your help is appreciated.
rubberjohn wrote:Would it be better (or possible) to use a join to do this or would it be better to split these into two different queries -
one to check if the current user has already submitted the tag and then a separate one to see if any other user has submitted that tag?
Although technically possible to do in one query, I, personally, would do this logic in two. A join isn't required to check if the tag exists, and the previous join will work against the current user for the second query.