insert-select

MySql Insert Select uuid()

☆樱花仙子☆ 提交于 2019-12-05 22:41:52
问题 Say you have a table: `item` With fields: `id` VARCHAR( 36 ) NOT NULL ,`order` BIGINT UNSIGNED NOT NULL And: Unique(`id`) And you call: INSERT INTO `item` ( `item`.`id`,`item`.`order` ) SELECT uuid(), `item`.`order`+1 MySql will insert the same uuid into all of the newly created rows. So if you start with: aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa, 0 bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb, 1 You'll end up with: aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa, 0 bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb, 1 cccccccc

MySql Insert Select uuid()

穿精又带淫゛_ 提交于 2019-12-04 03:12:58
Say you have a table: `item` With fields: `id` VARCHAR( 36 ) NOT NULL ,`order` BIGINT UNSIGNED NOT NULL And: Unique(`id`) And you call: INSERT INTO `item` ( `item`.`id`,`item`.`order` ) SELECT uuid(), `item`.`order`+1 MySql will insert the same uuid into all of the newly created rows. So if you start with: aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa, 0 bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb, 1 You'll end up with: aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa, 0 bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb, 1 cccccccc-cccc-cccc-cccc-cccccccccccc, 1 cccccccc-cccc-cccc-cccc-cccccccccccc, 2 How do I command MySql to create

MySQL Conditional INSERT

南楼画角 提交于 2019-12-02 13:12:15
问题 I want to do a conditional insert with MySQL. I have 2 tables (Car and CarType). The Car table got a coloumn called typeId , which points to an entry in the CarType table. I only want to insert a row in the Car table if the given typeId exists in the CarType table. I've tried some Googling, and tried a couple of solutions. Here is what I found (but it does not work): INSERT INTO Car (title, licensePlate, carType SELECT 'Ford Transit', 'SV 32 654', '13' FROM DUAL WHERE EXISTS (SELECT typeId

(One table) insert rows

99封情书 提交于 2019-12-02 05:12:27
问题 This question is the continuation of this one. I have the following table egr : +---------+------------+ | offid | groupid | +---------+------------+ | 1 | 101 | | 1 | 202 | | 2 | 202 | | 2 | 404 | +---------+------------+ I would like to insert missing groupids that the offid 2 does not have (compared to offid 1). Result would be: +---------+------------+ | offid | groupid | +---------+------------+ | 1 | 101 | | 1 | 202 | | 2 | 202 | | 2 | 404 | | 2 | 101 | --> new row to insert +---------+

MySQL Conditional INSERT

人盡茶涼 提交于 2019-12-02 03:25:19
I want to do a conditional insert with MySQL. I have 2 tables (Car and CarType). The Car table got a coloumn called typeId , which points to an entry in the CarType table. I only want to insert a row in the Car table if the given typeId exists in the CarType table. I've tried some Googling, and tried a couple of solutions. Here is what I found (but it does not work): INSERT INTO Car (title, licensePlate, carType SELECT 'Ford Transit', 'SV 32 654', '13' FROM DUAL WHERE EXISTS (SELECT typeId FROM CarType WHERE typeId = 13) I think you misleading by the example, it can be as simple as this :

SQLITE equivalent for Oracle's ROWNUM?

核能气质少年 提交于 2019-11-29 14:06:21
I'm adding an 'index' column to a table in SQLite3 to allow the users to easily reorder the data, by renaming the old database and creating a new one in its place with the extra columns. The problem I have is that I need to give each row a unique number in the 'index' column when I INSERT...SELECT the old values. A search I did turned up a useful term in Oracle called ROWNUM, but SQLite3 doesn't have that. Is there something equivalent in SQLite? You can use one of the special row names ROWID , OID or _ROWID_ to get the rowid of a column. See http://www.sqlite.org/lang_createtable.html#rowid

Get id from INSERT or SELECT

强颜欢笑 提交于 2019-11-28 14:29:35
I've this function that inserts a row into a city table without duplicates. It returns the id of the inserted row: CREATE OR REPLACE FUNCTION public.insert_city( character varying, character varying, character varying, character varying, character varying, character varying) RETURNS integer AS $BODY$ DECLARE name_city1 ALIAS FOR $1; country1 ALIAS FOR $2; province1 ALIAS FOR $3; region1 ALIAS FOR $4; cap1 ALIAS FOR $5; nationality1 ALIAS FOR $6; id_city1 integer; BEGIN INSERT INTO city (name_city, country, province, region, cap, nationality) SELECT name_city1, country1, province1, region1,

SQLITE equivalent for Oracle's ROWNUM?

夙愿已清 提交于 2019-11-28 07:36:56
问题 I'm adding an 'index' column to a table in SQLite3 to allow the users to easily reorder the data, by renaming the old database and creating a new one in its place with the extra columns. The problem I have is that I need to give each row a unique number in the 'index' column when I INSERT...SELECT the old values. A search I did turned up a useful term in Oracle called ROWNUM, but SQLite3 doesn't have that. Is there something equivalent in SQLite? 回答1: You can use one of the special row names

Get id from INSERT or SELECT

时光总嘲笑我的痴心妄想 提交于 2019-11-27 08:30:10
问题 I've this function that inserts a row into a city table without duplicates. It returns the id of the inserted row: CREATE OR REPLACE FUNCTION public.insert_city( character varying, character varying, character varying, character varying, character varying, character varying) RETURNS integer AS $BODY$ DECLARE name_city1 ALIAS FOR $1; country1 ALIAS FOR $2; province1 ALIAS FOR $3; region1 ALIAS FOR $4; cap1 ALIAS FOR $5; nationality1 ALIAS FOR $6; id_city1 integer; BEGIN INSERT INTO city (name

INSERT with SELECT

柔情痞子 提交于 2019-11-26 00:32:34
问题 I have a query that inserts using a select: INSERT INTO courses (name, location, gid) SELECT name, location, gid FROM courses WHERE cid = $cid Is it possible to only select \"name,location\" for the insert, and set gid to something else in the query? 回答1: Yes, absolutely, but check your syntax. INSERT INTO courses (name, location, gid) SELECT name, location, 1 FROM courses WHERE cid = 2 You can put a constant of the same type as gid in its place, not just 1, of course. And, I just made up the