1
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).

6
  • Possible duplicate of How to use 'select ' in MySQL 'insert' statement Commented Feb 4, 2018 at 20:51
  • @JoshuaR. In the link, you mentioned it will insert the same value from another row. But in my case, it should toggle between X and O. Commented Feb 4, 2018 at 20:59
  • It sounds like you want UPDATE rather than INSERT. INSERT makes a new row. Commented Feb 4, 2018 at 21:03
  • are there unique indexes ? Commented Feb 4, 2018 at 22:32
  • @JoshuaR. I don't want it with UPDATE. When I am inserting a new row, it should toggle the sign column value from the other row sign column value. Commented Feb 5, 2018 at 0:40

1 Answer 1

4

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

e.g.

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.