How to insert score with value and score_id ? At server im know only score_type.name and score.value. How i can insert new score with score_type ? If score_type name exsist just get id and insert, else create, get id and insert.
First try to create the score_type if it doesn't exist:
INSERT IGNORE INTO score_type (name) VALUES ("type_name");
Then use INSERT ... SELECT to insert the ID into the score table:
INSERT INTO score (value, score_type_id, player_id)
SELECT 123, id, "player_name"
FROM score_type
WHERE name = "type_name";
The first INSERT assumes you have a unique index on score_type.name. IGNORE means to fail silently if you try to insert a duplicate name.
Replace 123 and player_name with the known score.value and score.player_id.
If I understood your question correctly, you want to fill the score_type_id dynamically by selecting it using the name:
INSERT INTO `score`(`value`, `score_type_id`, `player_id`) VALUES (1337, (SELECT id FROM score_type WHERE score_type.name = "test") ,"maio290");
The trick is just to use another query instead of a fixed value.
Related
In my database mysql table, there is one field ,It was split by auto_increment id and other str , For example:
insert into tableName (id, title, link, keyword) values (NULL, 'Title', 'http://www.domain.com/id', 'keyword');
How could that work?
http://www.domain.com/id, this 'id' is the auto_increment id.
Unfortunately, you can't make it automatically since mysql for auto increment fields, doesn't know beforehand the inserted id. You could update the row after it is inserted:
INSERT INTO tableName (title, keyword) VALUES('Title', 'keyword');
UPDATE tableName
SET link = CONCAT('http://www.domain.com/', LAST_INSERT_ID())
WHERE id = LAST_INSERT_ID();
If id is auto_increment you don't need to set it on insert, just like this:
insert into tableName (title, link, keyword) values ('Title', 'http://www.domain.com/id', 'keyword');
In general if it auto incremented field, it can be your primary key and add other indexes as you need.
i got the following insert-command:
INSERT INTO PERSON ('Name','Age','Filename') VALUES ('Max',12,'Max_ID_.pdf');
i want to insert instead of 'Max_ID_.pdf' the string concartenated with the inserted id for this row. e.g.:
ID|Name|Age|Filename
2 |Max |12 |Max_2_.pdf
You can insert your row first, and than update it with the last inserted id:
INSERT INTO PERSON ('Name','Age','Filename') VALUES ('Max',12,'xxx');
UPDATE PERSON Filename=CONCAT(LAST_INSERT_ID(),'.pdf') where id = LAST_INSERT_ID()
There is also a way to do it in one statement, maybe a little more complex, and maybe will not work on every system, e.g. if you use innodb or transactions:
INSERT INTO PERSON SET Filename = CONCAT((SELECT auto_increment FROM
information_schema.tables WHERE table_name='PERSON'), '.pdf'),
Name = 'Max', Age = '12'
I have a simple table like this
CREATE TABLE authid(
id INT NOT NULL AUTO_INCREMENT,
authid VARCHAR(128) NOT NULL UNIQUE,
PRIMARY KEY(id)
);
Now if I insert a value with
INSERT INTO authid(authid) VALUES('test');
It will work fine and return the inserted id the first time, but if I do it again when the authid already exists (notice that we have authid marked as UNIQUE) it will return an error.
Is there a way achieve this this in one SQL statement: Insert it, get the id and if it already exists, still get the id.
Take a look at this: http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
If you're using MySQL 5.0 or higher you can use the "INSERT ... ON DUPLICATE KEY UPDATE" syntax. You may be able to combine that with LAST_INSERT_ID() (I'm not positive about that)
So:
insert into authid (authid) values ('test') on duplicate key update id=LAST_INSERT_ID(id), authid='test';
select LAST_INSERT_ID();
Well indeed if you try to insert 2 times the same value in a UNIQUE field, it won't work, that's the point of UNIQUE fields.
If I understand well, you want to know if it's possible whether to use an INSERT or an UPDATE statement depending on the existance of an item or not ? Then you need 2 queries, 1 to test existence, the other to insert new value or update existing one
Insert the value conditionally (i.e. if it doesn't exist). Whether the insert takes place or not, by the end of the statement the result will be the same: the value will be in the table. So, just select the ID of the row that matches that value. Or, speaking in SQL, like this:
INSERT INTO authid (authid)
SELECT 'test'
WHERE NOT EXISTS (
SELECT *
FROM authid
WHERE authid = 'test'
);
SELECT id
FROM authid
WHERE authid = 'test'
;
I would like do something like this in one only query.
REPLACE INTO table ( id, number ) VALUES ( 'test', number=number+5 )
What I want is (the first time!) insert the row and set the number 5.
the other times (if already exist) add 5 at the number.
Is it possible? I can't find nothing on line.
just be sure that ID is unique. Use INSERT ... ON DUPLICATE KEY UPDATE Syntax
INSERT INTO tableName (id, number)
VALUES ('test', 5)
ON DUPLICATE KEY UPDATE
number = number + 5
Assuming that id is a proper key (e.g. primary key):
INSERT INTO `table` (id, number)
VALUES ('test', 5)
ON DUPLICATE KEY UPDATE number=number+VALUES(number)
I have a table (foo) where I already have a PK on id:
id name rank
-----------------------
1 AAAA 2
2 BBBB 1
I want to insert a new row where I know the values of column id and name and want rank to take a value greater than any other value in the same column in preceding rows (similar to what auto_increment does for us).
i.e. if I were to add a row with value = CCCC, the rank column should have a value 3. I need to do this in a compound statement if possible. I tried the following which does not work.
insert into foo (`name`, `rank`)
values ('CCCC', (select max(`rank`) from `foo`))
Which gives me the following error:
You can't specify target table 'foo' for update in FROM clause
Note: I would ideally like to have the rank column as an auto_increment field, but apparently that's not allowed either, since I already have a PK.
PS: I need to be able to execute this statement from PHP without using stored procedures.
Try this first, it being derived instantly from your post:
INSERT INTO foo (`name`, `rank`)
SELECT 'CCCC', (MAX(`rank`) + 1) AS rank
FROM `foo`
Then using PDO, I think this'll work:
...
$sql = "INSERT INTO foo (`name`, `rank`) SELECT ?, (MAX(`rank`) + 1) AS rank FROM `foo`"
$name = "CCCC";
$st = $pd->prepare($sql);
$st->bindValue(1, $name);
try {
$retval = $st->execute();
} catch (PDOException $pdoex) {
...
Not sure if I got in syntactically correct but that should be about the gist of it ... I think
Err.. lemme know if the SQL works, at least :D