Can I have one query to check if an entry is in a mysql table, and if it is present, return a column, but if it's not present, insert it and return a value? I have a users table with the following layout:
userID - int, auto-increment
userName - varchar(100)
active - tinyint(1)
I want to do something like
select userID from users where userName='<some value>'
and if the userName exists, then return the userID, if not, insert it and return the newly incremented userID.
I know I can do this in two queries (one to check if userName exists, then another one to insert), but can I do it in one? I could create a stored procedure to only have one thing to call from my code, but the only way I can envision that stored procedure is to also do the two queries.
I see things like REPLACE INTO and ON DUPLICATE KEY UPDATE, but these only seem to work on updates, not on selecting a value.