MySQL query: how to insert with UNION?

I'm kind of new to mySQL: s union functions, at least when pasting with them. I got the following for work, based on an example found on the net:

INSERT INTO tableOne(a, b) SELECT a, $var FROM tableOne WHERE b = $var2 UNION ALL SELECT $var,$var 

Well, thereโ€™s nothing strange about that. But what happens when I want to insert a third value into a database that has nothing to do with the "Done" selection logic?

Like: INSERT INTO tableOne(a, b, c )

How can I do that?

+2
source share
1 answer

You can also "select" literal values:

 mysql> select 'hello', 1; +-------+---+ | hello | 1 | +-------+---+ | hello | 1 | +-------+---+ 1 row in set (0.00 sec) 

Therefore, you can also use this in INSERT INTO ... SELECT FROM and UNION s.

 INSERT INTO someTable (a, b, c) VALUES SELECT id, name, 5 FROM someOtherTable UNION SELECT id, alias, 8 FROM anotherTable 
+9
source

All Articles