query insert and select explaination - sql

I am confused about this query where it uses insert statement and then ignore and then select. Can someone explain me this? Thanks
INSERT IGNORE INTO
myTable
SELECT
$var1 AS `CMMNCTID`,
$var2 AS `ENCID`,
variableID,
ProductID,
CustomerID,
Age,
"" AS `myJ`,
"" AS `myI`
FROM
table2
JOIN
table3
ON
table2.ID= table3.ID

INSERT IGNORE in a keyword mysql: If the insert will cause a Primary key or Unique key error, it should skip that row.
INSERT ... SELECT: is used to copy data from another table.
http://dev.mysql.com/doc/refman/5.1/en/insert.html

The SELECT is a subquery. What the INSERT is doing is inserting whatever is contained in the join of table2 and table3, into myTable. The IGNORE tells it to ignore any entries for keys that already exist in myTable and to just insert whatever doesn't yet exist.
It seems the purpose of the query is to combine the data that is associated between table2 and table3 and dump it into myTable.

It takes results from the SELECT query on tables table2 and table3 and inserts the results into table myTable. IGNORE keyword simply tells mysql to ignore any errors that occur while executing the INSERT query.

Related

CTE: SELECT statement to read rows made by an INSERT Statement

I have a CTE that first inserts rows, and then reads the table with the inserted rows. Right now, the read on the table does not take into account the inserted rows.
The simplest example could be like this:
The Table:
CREATE TABLE mytable (column1 text, column2 text);
The query:
WITH insert_first AS (
INSERT INTO mytable (column1, column2)
VALUES ('value1', 'value2')
RETURNING *
), select_after AS (
SELECT * FROM mytable
LEFT JOIN insert_first ON insert_first.column1 = mytable.column1
) SELECT * FROM select_after
Here, select_after will be empty.
I thought by doing a LEFT JOIN on insert_first, I would hint to SQL to wait for the insert. But, it does not seem to do this.
Is there a way I could make a query that runs over mytable, which sees the inserts made from insert_first?
Here's a playground too: https://www.db-fiddle.com/f/4jyoMCicNSZpjMt4jFYoz5/6796
As Adrian mentioned in the comments, this is not possible:
From docs WITH: The primary query and the WITH queries are all (notionally) executed at the same time. This implies that the effects of a data-modifying statement in WITH cannot be seen from other parts of the query, other than by reading its RETURNING output. If two such data-modifying statements attempt to modify the same row, the results are unspecified.

Is the GROUP BY needed to insert

INSERT INTO NEWTABLE
(Street,
Number,
NuDate,
XValue)
SELECT
a1.Street,
a2.Number,
a2.NuDate,
a2.XValue
FROM
ABC.dbo.Faculty a1 INNER JOIN
ABC.dbo.Faculty2 a2
ON a1.NameID = a2.NameID
WHERE
a1.Bologna = 'True'
GROUP BY
a1.Street,
a2.Number,
a2.NuDate,
a2.XValue
In this completely fictitious SQL statement, is the GROUP by needed to insert properly into NEWTABLE? and/or does the group by need to match up perfectly with the INSERT INTO for this statement to work properly?
EDIT: Sorry, I realized I had the wrong values for the GROUP BY statement, they're supposed to match the INSERT INTO
In this completely fictitious SQL statement, is the GROUP by needed to insert properly into NEWTABLE?
It's not necessary if you don't mind duplicates
If you don't want duplicate rows then yes, you'll need to use GROUP BY (or DISTINCT):
SELECT DISTINCT
a1.Street,
a2.Number,
a2.NuDate,
a2.XValue
does the group by need to match up perfectly with the INSERT INTO for this statement to work properly?
Yes, the selected columns must match.
There are cases when what you group by doesn't match what you select but that's when you aggregating:
SELECT
column1, -- no aggregation, must match
sum(column2) -- aggregation, so does not need to match
FROM a
GROUP BY column1

Import data from SQL Server table into another table

I can't get this SQL statement right. I want to select band emails into another table but I don't understand what I'm doing wrong:
INSERT INTO submitted (mail)
SELECT
submitted.bandid,
band.mail,
band.id
FROM
band
WHERE
submitted.bandid = band.id
Can anybody help me?
Try this, you had specified 1 column to INSERT INTO but your SELECT query contained 3 columns, also the tables in your weren't joined:
INSERT INTO submitted (mail)
SELECT
band.mail
FROM band
INNER JOIN submitted ON submitted.bandid = band.id
You haven't mentioned the error message you're getting, but I suspect the problem is that you only have one column specified in the INSERT part of the statement, but the SELECT part returns many columns.
This w3schools page has a good example of how this query should be structured:
Copy only some columns from one table into another table:
INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;
I suspect you want update, not insert:
UPDATE s
SET mail = b.mail
FROM submitted s JOIN
band b
ON s.bandid = b.id;
insert inserts new rows into the table. update updates columns in existing rows.

How do I copy rows from one table to another in SQL?

I have been trying to copy the data listed in one table to another that are both located in the same database. However, every time I have everything entered, it runs the query and says 0 rows updated.
I have tried several variations in an attempt to get this to work. One such attempt is as listed below. I found this while researching in an attempt to get this done.
UPDATE
t1
SET
t1.column = t2.column
FROM
Table1 t1
INNER JOIN Table2 t2
ON t1.id = t2.id;
Any help on this would be greatly appreciated.
UPDATE will only modify records that already exist, if you want to insert rows that exist on another table use INSERT.
You can combine INSERT and SELECT to copy a whole table1 into a table2.
INSERT INTO table2 SELECT * FROM table1;
You can use INSERT IGNORE to copy only records that don't exist yet (based on unique keys).
You can also specify fields to copy in case the tables are different:
INSERT INTO table2 (id, first_name, last_name) SELECT id, name, surname FROM table1;

Delete all rows from a table that are present in a View

The queries I would like to perform:
BEGIN TRANSACTION
INSERT INTO TABLE_B SELECT * FROM TABLE_A WHERE SOME_COLUMN = 'something'
DELETE FROM TABLE_A WHERE COLUMN IN (
SELECT * FROM TABLE_A WHERE SOME_COLUMN = 'something'
)
END TRANSACTION
As you can see, there is a redundant SELECT statement in the DELETE query that I would like to replace (if possible), thus improving efficiency.
I was thinking to create a View with the rows in the first query and then scan the View with the rows in the second table. If some condition matches, then delete the row from the second table.
Could I get some pointers on how this could be done? If there is anything wrong I am doing, please criticize.
This is what you want, I think:
delete from table_a
where some_column = 'something'
As written, your query would probably generate a syntax error. The "SELECT *" would return all columns in table_a, and it presumably has more than one column.
Generally, you should be able to do joins in the delete statement, but you'd need to specify the table from which you're deleting rows:
delete table_a
from table_a left join table_b on (table_a.column_a = table_b.column_b)
where table_b.column_b = 'some_value'