2 Works:
INSERT INTO data._top
SELECT * FROM data.ops
WHERE ID = 'foo'
Works:
SELECT ID FROM data.table
How do I combine the above 2 statements using Mysql
INSERT INTO data._top
SELECT * FROM data.ops
WHERE (SELECT ID FROM data.table)
Are you possibly after something like this:
INSERT INTO `data._top`
SELECT * FROM `data.ops`
WHERE `id` IN (SELECT `id` FROM `data.table`);
Related
i have a table
users
id collection_id
1 xwkoss
2 cw2333
3 oipopp
And i run query:
SELECT * FROM USERS WHERE collection_id in ('xwkoss','cw2333', 'abcdeef')
that query work fine and return 2 values existing on table, but i need know, which value doesn't exist on table records example: 'abcdeef', based on search parameters
thanks
Create a table on-the-fly and check with NOT EXISTS:
SELECT user_code
FROM
(
SELECT 'xwkoss' AS user_code
UNION ALL
SELECT 'cw2333' AS user_code
UNION ALL
SELECT 'abcdeef' AS user_code
) codes
WHERE NOT EXISTS
(
SELECT NULL
FROM users
WHERE users.user_code = codes.user_code
)
ORDER BY user_code;
I have a table that looks like this:
serial|vehicule|alert_emails
12411|AAA|yes
12411|BBB|yes
13411|CCC|yes
13411|DDD|yes
14411|EEE|yes
I want to do a mysql query to select all data and organize it by serial field to get a array result like this:
12411
AAA|yes
BBB|yes
13411
CCC|yes
DDD|yes
14411
EEE|yes
I tried group by the field serial but I'm not getting the desired result:
SELECT * FROM mytable GROUP BY serial;
Any help please?
Thanks.
Use UNION ALL to get the distinct serials of the table and all the rows of the table:
SELECT CASE WHEN t.col IS NULL THEN t.serial END serial, t.col
FROM (
SELECT DISTINCT serial, null AS col
FROM mytable
UNION ALL
SELECT serial, CONCAT(vehicule, '|', alert_emails)
FROM mytable
) t
ORDER BY t.serial, t.col IS NULL DESC
See the demo.
I have imported several large csv's and I am looking to create a merged table from several imports.
So lets say I have 2 tables.
table1:
title
ben
rupert
table2:
title
karen
jill
and I want to either populate an empty table or create one on the fly.
//NewTable
title
ben
rupert
karen
jill
I've tried using SQL like this - but I am getting NewTable undefined variable issues
select *
into `NewTable`
from(
select * from `table1`
union all
select * from `table2`
union all
)t
Create NewTable first then:
INSERT INTO NewTable
SELECT * FROM table1
UNION ALL
SELECT * FROM table2
An alternate way in which you wouldn't need to create the table first, off the top of my head would be:
SELECT *
INTO NewTable
FROM table1
And then perform the insert from the second table:
INSERT INTO NewTable
SELECT * FROM table2
The select ... into ... statement in MySQL is for populating variables. What you are looking for is the insert ... select ... statement:
insert into newtable
select * from ...
insert into table1 select * from table2;
I want to have results in a table where the data comes from 3 different tables.
For that I have tried to execute this query:
INSERT INTO sometable (id,date)
VALUES
(
(SELECT id FROM table1
UNION
SELECT id FROM table2
UNION
SELECT id FROM table3)
,
(SELECT date FROM table1
UNION
SELECT date FROM table2
UNION
SELECT date FROM table3)
)
The result of this query is an error stating cannot insert multiple rows. Please help me to write this query correctly.
The INSERT ... SELECT syntax is different to the INSERT ... VALUES syntax. Also, you want to select both columns from each table at the same time:
INSERT INTO sometable (id, date)
SELECT id, date FROM table1 UNION
SELECT id, date FROM table2 UNION
SELECT id, date FROM table3
I have two tables with the same columns.
I can merge them with UNION
select * from table1
union
select * from table2;
How do I create a new table with the same contents and columns as that query?
You can use CREATE TABLE ... SELECT statement.
CREATE TABLE new_table
SELECT * FROM table1
UNION
SELECT * FROM table2;
create table new_table as
select col1, col2 from table1
union
select col1, col2 from table2
Make sure you select same set of columns from tables in union.
Or even you can explicitly define create table (we generally use in our project).
CREATE TABLE IF NOT EXISTS auditlog (
user_id varchar(30) NOT NULL default '',
user_ip varchar(255) NOT NULL default '',
........
........
KEY ie1 (user_id) )
union=(auditlog_2,auditlog_3,auditlog_4) engine=merge insert_method=last;