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
Related
I've searched around but can't find the answer I'm looking for.
I have two tables, each has a date field called last_update_date. I want to search which of the two tables has the most recent date and only return that date.
I tried this query hoping it will order the two tables last_update_date field and return that result but the query does not work. Any help would be appreciated.
"Select last_update_date from Table1, Table2 order by last_update_date DESC Limit 1"
SELECT MAX(last_update_date)
FROM (
SELECT MAX(last_update_date) as last_update_date
FROM Table1
UNION ALL
SELECT MAX(last_update_date)
FROM Table2
) tMax
select * from
( select last_update_date from table1
UNION ALL
select last_update_date from table2
) order by last_update_date;
SELECT MAX(A.last_update_date)
(SELECT last_update_date FROM Table1
UNION ALL
SELECT last_update_date FROM Table2) A;
table1
id id_customer name_photo1 photo_1 name_photo2 photo_2
1. 1 pic.png *sdhsadj jsjs.png *ssksksks
2. 2 pic2.png *sadjsad sdds.png *ssddsdsdw
To table2
id. id_customer namefoto picture
1. 1 pic.png *sdhsadj
2 1 jsjs.png *ssksksks
3. 2 pic2.png *sadjsad
4. 2 sdds.png *ssddsdsdw
As I can move the data with a query from the table to the table2, the column photo_1 and photo_2 is longblob.
Thanks
You need to "unpivot" to convert columns into records.
You can do it in one query with INSERT INTO .. SELECT in combination with UNION ALL
INSERT INTO
table2
(
table2.id_customer
, table2.namefoto
, table2.picture
)
SELECT
records.id_customer
, records.namefoto
, records.picture
FROM (
SELECT
table1.id_customer AS id_customer
, table1.name_photo1 AS namefoto
, table1.photo_1 AS picture
FROM
table1
UNION ALL
SELECT
table1.id_customer AS id_customer
, table1.name_photo2 AS namefoto
, table1.photo_2 AS picture
FROM
table1
) AS records
ORDER BY
records.customer_id ASC
You can do it with INSERT INTO ... SELECT statement:
INSERT INTO
table2(id_customer, namefoto, picture)
SELECT
id_customer,
name_photo1,
photo_1
FROM
table1;
INSERT INTO
table2(id_customer, namefoto, picture)
SELECT
id_customer,
name_photo2,
photo_2
FROM
table1;
I have 4 tables with various columns where ID being the common value in all 4 tables. There is a column called EventDate in all 4 tables but with different values or NULL values for the same ID. The new table should have only ID and EventDate.
I would like to compare all the 4 tables based on the EventDate (ie) if two tables have the same date, it should insert the min date into the new table.
Use UNION ALL to combine all and take Minimum date for every ID
SELECT ID, MIN(EventDate) AS MIN_DTE INTO NEW_TABLE FROM (
SELECT ID, EventDate from TABLE1
UNION ALL
SELECT ID, EventDate from TABLE2
UNION ALL
SELECT ID, EventDate from TABLE3
UNION ALL
SELECT ID, EventDate from TABLE4
)A
GROUP BY ID
I have 2 tables in MySQL DB.
Both the tables have a column as ID which is of type int(10) unsigned.
Table1 has no data and Table2 has the ID as 24.
I am using the below query to get the max ID
select max(ID) from
(
select IFNULL(max(ID),0) as ID from table1
UNION
select IFNULL(max(ID),0) as ID from table2
)temp;
I am expecting the value 24 but it gives 0.
Anything wrong in my query? Please help.
try this,
SELECT IFNULL(MAX(ID), 0) ID
FROM
(
SELECT ID FROM table1
UNION ALL
SELECT ID FROM table2
) a
insert into tblcustomermachine
(
select * from
((select vch_CustomerID from tblcustomer where tblcustomer.vch_CustomerID='Cust00001' )
union all
(select Rate from tblmachine)) as t );
that table contains 18 cols and this resultset also contains 18 rows yet it shows " Column count doesn't match value count at row 1" . why?
It looks like your table tblcustomermachine has more then the 1 column.
Like Simone answered, update your insert to INSERT INTO tblcustomermachine(col_1) SELECT ...
You may skip the column names during INSERT, however the SELECT needs to return the same amount of columns that the table holds.
AFAIK, you have to declare field name:
insert into tblcustomermachine (col_1, col_2, col_3, ... col_18) (
select t.field1, t.field2, t.field3, ... t.field18 from (
(select vch_CustomerID from tblcustomer where tblcustomer.vch_CustomerID='Cust00001')
union all (select Rate from tblmachine))
as t
);