MySQL - INSERT multiple values conditionally

guy_m

Ok, so I have a table which holds bets on games.
The table holds the columns: user_id, event_id, bet.
A user can send his/her (multiple) bets to the server in one request.

  • I need to insert multiple bets using one query, while checking that none of the bets are on an event that already started/finished.
  • In case of at least 1 started/finished event, I don't really care if the whole query cancels, or just ignores the 'unqualified' bets.

Question
How can I insert multiple bets (rows) with one query, while conditioning the insert on a select statement (which checks for each of the events' statuses)?

Here is the query I would've used if it worked (and it doesn't of course):

INSERT INTO bet_on_event (user_id, event_id, bet)
VALUES (1,5,1), (1,6,2)
IF (SELECT COUNT(*) FROM events WHERE _id IN(5,6) AND status=0) = ?;  

Explanation
1. As mentioned, the values are pre-made - requested by the user.
2. Games/events have status. 0 means a game hasn't started, so it's ok to bet.
3. The select statement just counts how many of the requested events have status 0.
4. The 'IF' should check if the count from (3) equals the number of events the user requested to bet on, thus confirming that all the events are ok to bet on.

The 'IF' should be replaced with something that work, and the whole statement can be replaced if you have a better idea for what I'm trying to achieve.

A simpler query (which isn't enough for my case, but works with 1 row) is:

INSERT INTO bet_on_event (user_id, event_id, bet)
SELECT 1,5,1 FROM dual
WHERE (SELECT COUNT(*) FROM events WHERE _id IN(5,6) AND status=0) = ?;  

Any idea? Is this even possible? Betting is gonna be used a lot, so I want to do it as quick as possible - with 1 query.
Thank you so much.

EDIT
That is what I ended up doing, taken from Thorsten's answer (I changed it to a dynamically built query, as that is what I need):

var query='INSERT INTO bet_on_event (user_id, event_id, bet)';

    for(var i=0; i<eventIds.length; i++){
    query+= ' SELECT ' + userId + ',' + eventIds[i] + ',' + bets[i] 
       + ' FROM dual WHERE EXISTS (SELECT * FROM events WHERE id = ' + eventIds[i]
       + ' AND Status = 0)';
        if(i < eventIds.length - 1){  
             query += ' UNION ALL';
        }
    }  

Where eventIds and bets are in a corresponding order (like a map)

EDIT 2
I also wanted to UPDATE the bets which already exist (in case the user wanted to...). So there's a need to update each row with the relevant bet in the array. This is the solution:

ON DUPLICATE KEY UPDATE bet=VALUES(bet)  

Just added (concatenated) to the end of the query...

Thorsten Kettner

Does this work for you? It inserts 1,5,1 if there is no event for id 5 that has started. Same for 1,6,1 and id 6.

INSERT INTO bet_on_event (user_id, event_id, bet)
SELECT 1,5,1 FROM dual WHERE NOT EXISTS 
  (SELECT * FROM events WHERE _id = 5 AND Status <> 0)
UNION ALL
SELECT 1,6,1 FROM dual WHERE NOT EXISTS 
  (SELECT * FROM events WHERE _id = 6 AND Status <> 0);

EDIT: If you don't want to insert anything in case one or more of the games have started, you can simply replace WHERE _id = 5 and WHERE _id = 6 with WHERE _id IN (5,6). Or have just one exists clause:

INSERT INTO bet_on_event (user_id, event_id, bet)
SELECT *
FROM
(
  SELECT 1,5,1 FROM dual
  UNION ALL
  SELECT 1,6,1 FROM dual
) tmp
WHERE NOT EXISTS (SELECT * FROM events WHERE _id IN (5,6) AND Status <> 0);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySql Multiple insert with select

From Dev

PDO Insert multiple checkbox values in Mysql

From Dev

PHP : insert multiple check boxes values into one MySQL column

From Dev

how to insert multiple of values in a single field in mysql

From Dev

Multiple INSERT/UPDATE on MySQL

From Dev

Insert multiple values in one column of mysql database

From Dev

Mysql multiple insert query

From Dev

Joining array values into string in MySQL multiple insert

From Dev

Multiple conditionally GROUP BY and COUNT using PHP and mysql

From Dev

Mysql Insert Multiple Rows with comma separated values with sql statement alone

From Dev

MySQL insert into values (select)

From Dev

Best way to insert multiple values in to DB - PHP MySQL

From Dev

To insert multiple check box values into separate rows in mysql

From Dev

Java streams adding multiple values conditionally

From Dev

How to insert values from JSON data with multiple objects into rows in MySQL?

From Dev

Java streams adding multiple values conditionally

From Dev

Insert values in mysql

From Dev

Insert multiple values via PHP PDO into MySQL database

From Dev

MySQL - INSERT multiple values conditionally

From Dev

Insert multiple arrays into mysql

From Dev

MySql can insert the values

From Dev

mysql insert values into table?

From Dev

MySQL insert data with fixed values and multiple select results

From Dev

Insert values from multiple arrays into MySQL

From Dev

insert multiple values in database

From Dev

Joining array values into string in MySQL multiple insert

From Dev

To insert multiple check box values into separate rows in mysql

From Dev

how to insert multiple text box with file uploader values in mysql database?

From Dev

Insert multiple values from API into mysql database

Related Related

  1. 1

    MySql Multiple insert with select

  2. 2

    PDO Insert multiple checkbox values in Mysql

  3. 3

    PHP : insert multiple check boxes values into one MySQL column

  4. 4

    how to insert multiple of values in a single field in mysql

  5. 5

    Multiple INSERT/UPDATE on MySQL

  6. 6

    Insert multiple values in one column of mysql database

  7. 7

    Mysql multiple insert query

  8. 8

    Joining array values into string in MySQL multiple insert

  9. 9

    Multiple conditionally GROUP BY and COUNT using PHP and mysql

  10. 10

    Mysql Insert Multiple Rows with comma separated values with sql statement alone

  11. 11

    MySQL insert into values (select)

  12. 12

    Best way to insert multiple values in to DB - PHP MySQL

  13. 13

    To insert multiple check box values into separate rows in mysql

  14. 14

    Java streams adding multiple values conditionally

  15. 15

    How to insert values from JSON data with multiple objects into rows in MySQL?

  16. 16

    Java streams adding multiple values conditionally

  17. 17

    Insert values in mysql

  18. 18

    Insert multiple values via PHP PDO into MySQL database

  19. 19

    MySQL - INSERT multiple values conditionally

  20. 20

    Insert multiple arrays into mysql

  21. 21

    MySql can insert the values

  22. 22

    mysql insert values into table?

  23. 23

    MySQL insert data with fixed values and multiple select results

  24. 24

    Insert values from multiple arrays into MySQL

  25. 25

    insert multiple values in database

  26. 26

    Joining array values into string in MySQL multiple insert

  27. 27

    To insert multiple check box values into separate rows in mysql

  28. 28

    how to insert multiple text box with file uploader values in mysql database?

  29. 29

    Insert multiple values from API into mysql database

HotTag

Archive