SQL query statements question
#5

(Untested)

Select or Insert ( https://dev.mysql.com/doc/refman/5.7...functions.html , https://dev.mysql.com/doc/refman/5.7/en/case.html )
Код:
SELECT CASE WHEN (SELECT COUNT(*) FROM player_stats WHERE id = 5 LIMIT 1)>0 
THEN 
    (SELECT * FROM player_stats WHERE id = 5 LIMIT 1) 
ELSE        
    INSERT INTO player_stats (id, name, etc) VALUES (1, 'Meller', 'etc etc')
END
Insert if not exists ( https://dev.mysql.com/doc/refman/5.7...ubqueries.html )
Код:
INSERT INTO player_stats (id, name, etc)
SELECT * FROM (SELECT 'Meller', 'etc etc') AS tmp
WHERE NOT EXISTS (
    SELECT * FROM player_stats WHERE id = 5 LIMIT 1)
) LIMIT 1;
I am not sure whether it will cause mysql errors, but you can search on ****** for some answers or explore the MySQL documentation.

There is also INSERT ... ON DUPLICATE KEY UPDATE
Reply


Messages In This Thread
SQL query statements question - by Meller - 09.09.2017, 15:02
Re: SQL query statements question - by Paulice - 09.09.2017, 15:11
Re: SQL query statements question - by Meller - 09.09.2017, 15:17
Re: SQL query statements question - by Xeon™ - 09.09.2017, 15:34
Re: SQL query statements question - by Kaperstone - 09.09.2017, 15:36
Re: SQL query statements question - by Meller - 09.09.2017, 15:54
Re: SQL query statements question - by Vince - 09.09.2017, 16:19

Forum Jump:


Users browsing this thread: 1 Guest(s)