简体   繁体   中英

SQL INSERT row based on another row with same column value

SNO | BOARD        | PLAYER_ID     | SIGN
1   | xyz          | 1             | X
2   | xxx          | 1             | O

I have created a similar table for the game tic tac toe and I want to insert row based on another row with the same column value.

For example:

If I send BOARD = xyz and PLAYER_ID = 2 then MySQL should consider SIGN = O

I can use ENUM to only accept 2 values for the SIGN column to accept X or O values (Suggest me for better ideas).

Find the INSERT syntax here:

https://dev.mysql.com/doc/refman/5.7/en/insert.html

their example

INSERT INTO tbl_temp2 (fld_id)
SELECT tbl_temp1.fld_order_id
FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;

If you combine that with an IF function in the SET part of the above UPDATE , see:

https://dev.mysql.com/doc/refman/5.7/en/control-flow-functions.html

eg

IF(SIGN='X,'O','X')

you should be able to toggle pieces for players and games.

You'll also need some way to identify the most recent move. If SNO is intended to be a unique incrementing move identifier, MAX(SNO) +1 can be used to increment it when you add rows.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM