insert-into

Insert New Row in Table 3 if combination of Col A and Col B in Table C Don't Exist

a 夏天 提交于 2019-12-06 07:58:29
I have a code that gets data from Tables 1 and 2, and then inserts new rows into Table 3. My problem is that the code adds records that already exist. How can I prevent duplicate errors from being inserted, when the combination of groupid and userid in Table C already exists? INSERT INTO mdl_groups_members (groupid,userid) SELECT l.mgroup AS moodle, r.id AS mdl_user FROM moodle AS l JOIN mdl_user AS r ON l.orders_id = r.id WHERE l.mgroup > 0 Here's the table before I ran the script: id groupid userid timeadded 1 1 1 1372631339 2 4 2 1372689032 3 8 3 1373514395 4 3 4 1373514395 Here's the table

SQL DML: Incorrect date value (MySQL)

我是研究僧i 提交于 2019-12-06 02:35:08
问题 I created a table in my database: CREATE TABLE official_receipt( student_no INT UNSIGNED, academic_year CHAR(8), trimester ENUM('1', '2', '3'), or_no MEDIUMINT UNSIGNED, issue_date DATE NOT NULL, received_from VARCHAR(255) NOT NULL, amount_of DECIMAL(8,2) NOT NULL, issued_by VARCHAR(255), doc_type ENUM('FULL', 'DOWN', 'INST') NOT NULL, form_of_payment ENUM('CASH', 'INST') NOT NULL, PRIMARY KEY (student_no, academic_year, trimester, or_no) ); I inserted some values: INSERT INTO official

Does INSERT INTO … SELECT … always match fields by ordinal position?

 ̄綄美尐妖づ 提交于 2019-12-05 10:46:32
问题 My tests seem to confirm that INSERT INTO a (x, y) SELECT y, x FROM b maps b.y to a.x , i.e., the fields are matched only by ordinal position and not by name. Is this always the case, i.e., can I rely on that behaviour? Unfortunately, the documentation does not specify this (or I didn't find it). 回答1: That is correct, SQL Server does not try to do any mapping of column names since you can apply any aliases to the source data that you like. It will always reference ordinal position. 回答2: Yes,

SQL Server: Select from two tables and insert into one

故事扮演 提交于 2019-12-04 19:39:20
I have a stored procedure with two table variables (@temp and @temp2). How can I select the values from both temp tables (Both table variables contain one row) and insert them all in one table ? I tried the following but this didn't work and I got the error that the number of SELECT and INSERT statements does not match. DECLARE @temp AS TABLE ( colA datetime, colB nvarchar(1000), colC varchar(50) ) DECLARE @temp2 AS TABLE ( colD int ) ... INSERT INTO MyTable ( col1, col2, col3, col4 ) SELECT colD FROM @temp2, colA FROM @temp, colB FROM @temp, colC FROM @temp Many thanks for any help with this,

Inserting records into a MySQL table using Java

橙三吉。 提交于 2019-12-04 17:10:00
问题 I created a database with one table in MySQL: CREATE DATABASE iac_enrollment_system; USE iac_enrollment_system; CREATE TABLE course( course_code CHAR(7), course_desc VARCHAR(255) NOT NULL, course_chair VARCHAR(255), PRIMARY KEY(course_code) ); I tried to insert a record using Java: // STEP 1: Import required packages import java.sql.*; import java.util.*; public class SQLInsert { // JDBC driver name and database URL static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final

SQL DML: Incorrect date value (MySQL)

强颜欢笑 提交于 2019-12-04 08:14:47
I created a table in my database: CREATE TABLE official_receipt( student_no INT UNSIGNED, academic_year CHAR(8), trimester ENUM('1', '2', '3'), or_no MEDIUMINT UNSIGNED, issue_date DATE NOT NULL, received_from VARCHAR(255) NOT NULL, amount_of DECIMAL(8,2) NOT NULL, issued_by VARCHAR(255), doc_type ENUM('FULL', 'DOWN', 'INST') NOT NULL, form_of_payment ENUM('CASH', 'INST') NOT NULL, PRIMARY KEY (student_no, academic_year, trimester, or_no) ); I inserted some values: INSERT INTO official_receipt(student_no , academic_year, trimester, or_no, issue_date, received_from, amount_of, issued_by, doc

Rails 3.2 + MySQL: Error: Field 'created_at' doesn't have a default value: INSERT INTO

空扰寡人 提交于 2019-12-03 23:31:58
I created a new migration, where is mentioned ... t.timestamps in the created table are added these two columns ... | created_at | datetime | NO (Null) | | NULL (Default) | | updated_at | datetime | NO (Null) | | NULL (Default) | ... When I want to create a new item, I always get the error message Mysql2::Error: Field 'created_at' doesn't have a default value: INSERT INTO `table_name` (`first_col`, `second_col`) VALUES ('a', 'b') Am I missing something? I sent this miniapp to a friend of mine and he is able to run successfully run it -> the record is created in database. What am I missing? I

Inserting records into a MySQL table using Java

喜你入骨 提交于 2019-12-03 12:26:12
I created a database with one table in MySQL: CREATE DATABASE iac_enrollment_system; USE iac_enrollment_system; CREATE TABLE course( course_code CHAR(7), course_desc VARCHAR(255) NOT NULL, course_chair VARCHAR(255), PRIMARY KEY(course_code) ); I tried to insert a record using Java: // STEP 1: Import required packages import java.sql.*; import java.util.*; public class SQLInsert { // JDBC driver name and database URL static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost:3306/iac_enrollment_system"; // Database credentials static final

SELECT .. INTO returns more than one rows - Mysql

删除回忆录丶 提交于 2019-12-02 17:02:24
问题 i have a basic stored procedure DECLARE user_o VARCHAR(50); SELECT user_name INTO user_o FROM users WHERE topic_id = 54 AND entry_time BETWEEN 2017-09-17 AND date_add( CURRENT_DATE, INTERVAL 1 DAY) ORDER BY entry_time ASC LIMIT 10; this throws me error #1172 sql returns more than one rows. no idea why? my goal is to have this result set user_name | user_o mike mike liz liz helen helen her her 回答1: Its because you are inserting the result 'INTO' a variable and because the result is more than a

Enforce a foreign-key constraint to columns of same table

时间秒杀一切 提交于 2019-11-30 07:27:29
问题 How to enforce a constraint of foreign key on columns of same table in SQL while entering values in the following table: employee : empid number, manager number (must be an existing employee) 回答1: CREATE TABLE TABLE_NAME ( `empid_number` int ( 11) NOT NULL auto_increment, `employee` varchar ( 100) NOT NULL , `manager_number` int ( 11) NOT NULL , PRIMARY KEY (`empid_number`), CONSTRAINT `manager_references_employee` FOREIGN KEY (`manager_number`) REFERENCES (`empid_number`) ) ENGINE=InnoDB