I have a two tables A and B
I would like to union them and store in to another table;
CREATE TABLE myspace.test (
(select * from A ) UNION ( select * from B) );
It fails with an error
You have an error in your SQL syntax;
check the manual that corresponds to
your MySQL server version for the
right syntax to use near 'CREATE TABLE
myspace.test ( (select * from A )
UNION ( s' at line 1
But the query with: (select * from A ) UNION ( select * from B) gives correct result.
How to store union result in to another table??
Thanks Arman.
EDIT
Well After playing around I found that:
The query without outer brackets works.
CREATE TABLE myspace.test
(select * from A ) UNION ( select * from B) ;
Adding AS is not solving the problem.
I was wondered that query with brackets is working well seems tome BUG or maybe I am missing something?
CREATE TABLE myspace.test
(select * from A);
CREATE TABLE
myspace.test
AS
SELECT *
FROM A
UNION
SELECT *
FROM B
Read the documentation. There are no parens around the select-statement.
CREATE TABLE `myspace`.`test` (SELECT * FROM `A`) UNION (SELECT * FROM `B`);
Watch out for duplicate primary keys, though. You may want to consider first creating an empty table myspace.test with the proper layout, then inserting rows into it more selectively.
This does not look right at first sight.
Start by simply creating your new empty table using CREATE TABLE.
Then run a query to populate it, should be something like
INSERT INTO newTable(field1, field2,..., fieldN)
SELECT temp.field1, temp.field2,...,temp.fieldN
FROM
(
SELECT field1, field2,...,fieldN
FROM A
UNION
SELECT field1, field2,...,fieldN
FROM B
) temp
Hope this helps!
Related
I'm trying to generate a triangle of stars in MYSQL that looks like this:
*****
****
***
**
*
I'm using the following code to do in MYSQL
with recursive print_star(n) as (
select '*'
UNION ALL
select concat(n,'*')
from print_star
where length(n)<5
)
select * from print_star order by length(n) desc
I get the error "Data too long for column 'n' at row 1". Can anybody help me find out what's wrong?
I guess MySQL is finicky about types. Try this:
with recursive print_star(n) as (
select cast('*' as char(255)) n
union all
select concat(n, '*')
from print_star
where length(n) < 5
)
select *
from print_star
order by length(n) desc
My dbm knowledge is still pretty limited, so I am not sure how to approach/solve this problem. I want to INSERT INTO one of two tables, say, table1 and table2, but I don't know which table until after a SELECT subquery. Something like this:
INSERT INTO (SELECT tblname) SELECT *, IF(somecondition, 'table1', 'table2') as tblname FROM `anothertable` WHERE id = 'someid'
I tried this as a test:
INSERT INTO (SELECT tblname) SELECT *, 'table1' as tblname FROM `anothertable` WHERE id = 'someid'
But that didn't work.
I know I can use subqueries in SELECT statements (so useful!), and that I can technically achieve what I want with NOT EXISTS in 2 statements, and I know I cannot INSERT into two tables, and I know that using # user variables is unreliable within a statement (see docs). So, is there a way to achieve what I want, in a single statement?
you can do it with case statement.
SELECT CASE WHEN ( SELECT IF(somecondition, 'table1', 'table2') as tblname FROM `anothertable` = 'table1' )
THEN <QUERY A>
ELSE <QUERY B>
END
How do i create a new Hive table from many Hive tables? I used JSON strings to create the original table and not sure how to create the new one.
create table Bank_F001_table as
select
get_json_object(Bank_F001.json, '$.text') as text,
get_json_object(Bank_F001.json, '$.coordinates') as coordinates,
get_json_object(Bank_F001.json, '$.user.location') as location,
get_json_object(Bank_F001.json, '$.lang') as lang,
get_json_object(Bank_F001.json, '$.user_mentions') as user_mentions,
get_json_object(Bank_F001.json, '$.user.screen_name') as screen_name,
get_json_object(Bank_F001.json, '$.user.name') as name,
get_json_object(Bank_F001.json, '$.listed_count') as listed_count,
get_json_object(Bank_F001.json, '$.in_reply_to_user_id_str') as in_reply_to_user_id_str
from Bank_F001;
Just wanted to add, i used UNION ALL to select attributes which worked out successfully. But I am unable to create a new table to use for other activities. Oddly, I also tried UNION (not with ALL) and Hive told me i need to use ALL.
Here is my Select Code....
select *
from BMO_F016_table
union all
select *
from BMO_F017_table
union all
select *
from BMO_F018_table
union all
select *
from BMO_F054_table ) as large_table
group by screen_name
order by cnt desc;
For some reason, it wasn't working but i figured it out... in case you're interested, here you go...
CREATE TABLE BMO_Table as
SELECT *
FROM BMO_F016_table
UNION ALL
SELECT *
FROM BMO_F017_table
UNION ALL
SELECT *
FROM BMO_F018_table
UNION ALL
SELECT *
FROM BMO_F054_table
UNION ALL
SELECT *
FROM BMO_F093_table
;
Is it possible to join Two queries of mysql in a query ??
Like:
select * from a + select * from b
So that I can use them in a single php loop.
If they have the same number of columns and the datatypes are the same in each column, then you can use a UNION or UNION ALL:
select *
from a
UNION ALL
select *
from b
If you provide more details about the tables, data, etc, then there might be another way of returning this data.
A UNION will return only the DISTINCT values, while a UNION ALL selects all values.
If this is the route that you need to take, and you still need to identify which table the data came from, then you can always create a column to identify which table the data is from , similar to this:
select *, 'a' TableName
from a
UNION ALL
select *, 'b' TableName
from b
This allows you to distinguish what table the data came from.
I think it is easier creating sql "variables" like:
select varA, varb from TableA, tableB;
and you can just play with values in PHP accessing properties.
That way you can take conditions in the query like:
select varA, varb from TableA, tableB where varA.id = varB.foreingId bla bla...
;)
I have a function in a module (the function combines 6 tables into a new one, then looks up another table to update one of the field values in the new table) and all is working perfectly well. Now I would like to call this function from an access 2003 query so that when i run the query the New Table should be opened.
I tried using Expr: in the query but although the new table is being created in the 'Tables' Section (by invoking my function), all I can see when i run the query is a blank table with one column named 'Expr'.
Can anyone please guide me to the right direction?
Any help would be greatly appreciated.
You can use in your select ..
SELECT * INTO TBL_ALLTABLES
FROM (
--Here your select union all tables for example
SELECT * from tbl1
UNION ALL
SELECT * from tbl2
UNION ALL
SELECT * from tbl3
UNION ALL
SELECT * from tbl5
UNION ALL
)