Reputation: 10228
I have a query like this:
INSERT INTO votes (post_id, user_id, value, date_time)
SELECT ?,?,?,?,?,?
FROM $table_name t
WHERE {if t.id exists $id} limit 1
How can I write this {if t.title exists 'anythin'}
in MySQL ?
EDIT: Actually I want to check post
is exists in the Posts
table before inserting a vote for that post.
Upvotes: 1
Views: 1902
Reputation: 3311
Have you tryed
WHERE EXISTS (SELECT * FROM $Posts_table_name WHERE Id = 'YourPostId');
Edit: You wrote: " Actually I want to check post is exists in the Posts table before inserting a vote for that post."
Best way to do this is to set post_id as foreign key so, if the id doesn't exist the insert query will not have effects
Upvotes: 1
Reputation: 964
If your goal is to select 'where' data exists, try WHERE t.title IS NOT NULL
.
If it is to complete a conditional statement which inserts 'anything' if there is any data, then try (if t.title is not null, 'anything', null)
Upvotes: 2