Is there a way to make this statement into one? The basic idea is to insert a tag name if it doesnt exist and to retrieve the primary key for my next statement. title column is unique.
INSERT INTO `tag_name` (`count`, `title`)
SELECT 0, @title FROM DUAL
WHERE not exists
(SELECT * FROM `tag_name` WHERE `title` = @title LIMIT 1);
If rows affect >0 then use last_insert_rowid(), otherwise run this statement
SELECT id from FROM `tag_name` WHERE `title` = @title
--- here is a test
CREATE TABLE test (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, data int unique);
//insert into test(data) select 5;