How Can I Check Two Conditions Before Inserting?
I have three tables: // Posts +----+----------+---------------+-----------+ | id | title | content | id_author | +----+----------+---------------+-----------+ | 1 | title
Solution 1:
INSERTINTO Votes (id_post,id_user)
SELECT p.id,u.id
FROM Posts p, Users u
WHERE p.id_user = :author
AND u.id = :userAND u.active =1 limit 1;
then you set parameter user equal to the current user id.
EDIT: I suppose id_user in table Votes must be the voter's id, not the author of the post (correct?), so I fixed the query eliminating the JOIN.
Solution 2:
Use and with where
INSERTINTO Votes (id_post,id_user)
SELECT ?,?
FROM Posts p, Users u
WHERE p.id_user = :author and u.active =1 limit 1;
Solution 3:
INSERTINTO Votes (id_post,id_user)
SELECT p.id, u.id
FROM Posts p
INNERJOIN Users u ON1=1WHERE p.id_user = :author
AND u.id = :current_user_id
AND u.active =1
LIMIT 1;
Post a Comment for "How Can I Check Two Conditions Before Inserting?"