开发者

MySQL one column equal another?

开发者 https://www.devze.com 2023-02-07 17:08 出处:网络
How in php/mySQL can i make a column equal another column? id is auto increment, xid should be unique. Can i make xid = id in SQL? (the reason for this is to log changes but i\'ll explain more if nee

How in php/mySQL can i make a column equal another column?

id is auto increment, xid should be unique. Can i make xid = id in SQL? (the reason for this is to log changes but i'll explain more if needed)

To make xid unique the best way is to copy the auto increment of id

+----+-----+----------+----------------+------+---------+
| id | xid | title    | body           | page | visible |
+----+-----+----------+----------------+------+---------+
|  1 |  1  | my title | my body        | NULL |  开发者_如何学JAVA     1 | 
|  2 |  2  | my title | my body edited |    1 |       0 | 
+----+-----+----------+----------------+------+---------+

$queryX = "INSERT INTO table (xid, title, body, page, visible) 
VALUES (, 'Plays', 'it's playing', 'book page', 1)";


I don't understand why you want to do this, but my approach would be using two queries and LAST_INSERT_ID():

INSERT INTO table (title, body, page, visible) 
VALUES ('Plays', 'it''s playing', 'book page', 1);
UPDATE table SET xid = id WHERE id = LAST_INSERT_ID();

You may want to do this using a TRIGGER, so you don't have to do it manually.


In one query:

INSERT INTO table (xid, title, body, page, visible) VALUES 
( SELECT MAX(xid) + 1, .... )

it will put unique xid, can be done with id also, I not quite sure I undestand the need...

0

精彩评论

暂无评论...
验证码 换一张
取 消