I have a access database which includes tblSO and tblSO_archive tables.
I want to combine this two tables. The tables have the same columns and column names.
I tried INTERSECT and UNION but it doesn't work for me. Is there any suggestion? When I try UNION it gives me too many columns error.
Table 1
Table 2
Create dummy fields for the ones that don't have a corresponding match with the other table.
SELECT strSO, strTSN FROM Table1
UNION
SELECT strSO, '' FROM Table2
Related
I have a mysql database and I need to merge two columns together, however I need to merge the columns in such a way that they are not just concatenated together but instead a whole new separate record.
Example:
Before Merge:
name Computer-one Computer-two
jack hp dell
<insert unkown sql statement (what I need)>
After Merge
name Computer-one
jack hp
jack dell
When I try to merge the two columns they simply merge in to a single column but do not repeat the record instead (look above):
name Computer-one
jack hpdell
Currently I am using the concat statement. Which is the only way I've been able to get the items to merge. These columns can contain null values, there is a ID that auto increments for the primary key.
The problem is that you actually dont want to merge them.
You want to create new rows for a field.
You can just do this.
INSERT INTO table_name(name, Computer-one)
SELECT
name, Computer-two
FROM
table_name;
Then you will create new rows for all rows with there Computer-two values put into the Computer-one field.
When you see that your table has twice as many rows and the Computer-one field is correct you can just drop the 'Computer-two' column and the data is as you wanted.
Always make a backup first or try on test-tables so that you know that everything works first.
I think you just need UNION ALL statement. You may try below -
SELECT name, "Computer-one"
FROM YOUR_TABLE
WHERE "Computer-one" IS NOT NULL
UNION ALL
SELECT name, "Computer-two"
FROM YOUR_TABLE
WHERE "Computer-two" IS NOT NULL;
So you need to do two queries on this table and union the result. Insert results into temp table. Then select distinct records for each record for column 'nane'
SELECT INTO table2(name, model)
SELECT A.name, A.computer-one AS model from table as A
UNION
SELECT B.name, B.computer-two AS model from table as B
I want to get all data from two table on different Databases.I've tried this and only get data from one table.
Select b1.branch.*, db2.branch.* from db1.branch,db2.branch
Is there any way to get that?
Assuming that your branch table have the same struct in both database then you could use an union eg:
Select db1.branch.*
from db1.branch
union all
select db2.branch.*
from db2.branch
for obtain both result sets
I'm using MySQL and I selected three tables in one statement like that:
SELECT * FROM tb_i, tb_s, tb_t
But I need case control by their table names. If a row from tb_s it returns tb_s on additional column table_name.
How can I get table name from this statement?
The way you add up your tables now is a JOIN - meaning that every row is a Cartesian product of three of them - so each row is returned from all the three tables.
If you would like to concatenate the data from the three tables you should use UNION ALL which assumes that the columns are of the same structure. Then you would able to mark the origin table, with an addition constant field.
For instance in your case:
SELECT tb_i.*,'tb_i' as source
FROM tb_i
UNION ALL
SELECT tb_s.*,'tb_s'
FROM tb_s
UNION ALL
SELECT tb_t.*,'tb_t'
FROM tb_t
where the source column is a constant string per each table.
I have two tables.
I query like this:
SELECT * FROM (
Select requester_name,receiver_name from poem_authors_follow_requests as one
UNION
Select requester_name,receiver_name from poem_authors_friend_requests as two
) as u
where (LOWER(requester_name)=LOWER('user1') or LOWER(receiver_name)=LOWER('user1'))
I am using UNION because i want to get distinct values for each user if a user exists in the first table and in the second.
For example:
table1
nameofuser
peter
table2
nameofuser
peter
if peter is on either table i should get the name one time because it exists on both tables.
Still i get one row from first table and a second from table number two. What is wrong?
Any help appreciated.
There are two problems with your SQL:
(THis is not the question, but should be considered) by using WHERE over the UNION instead of the tables, you create a performance nightmare: MySQL will create a temporary table containing the UNION, then query it over the WHERE. Using a calculation on a field (LOWER(requester_name)) makes this even worse.
The reason you get two rows is, that UNION DISTINCT will only suppress real duplicates, so the tuple (someuser,peter) and the tuple (someotheruser, peter) will result in duplication.
Edit
To make (someuser, peter) a duplicate of (peter, someuser) you could use:
SELECT
IF(requester_name='peter', receiver_name, requester_name) AS otheruser
FROM
...
UNION
SELECT
IF(requester_name='peter', receiver_name, requester_name) AS otheruser
FROM
...
So you only select someuser which you already know : peter
You need the where clause on both selects:
select requester_name, receiver_name
from poem_authors_follow_requests
where LOWER(requester_name) = LOWER('user1') or LOWER(receiver_name) = LOWER('user1')
union
select requester_name, receiver_name
from poem_authors_friend_requests
where LOWER(requester_name) = LOWER('user1') or LOWER(receiver_name) = LOWER('user1')
The two queries are independent of each other, so you shouldn't try to connect them other than by union.
You can use UNION if you want to select rows one after the other from several tables or several sets of rows from a single table all as a single result set.
UNION is available as of MySQL 4.0. This section illustrates how to use it.
Suppose you have two tables that list prospective and actual customers, a third that lists vendors from whom you purchase supplies, and you want to create a single mailing list by merging names and addresses from all three tables. UNION provides a way to do this. Assume the three tables have the following contents:
http://w3webtutorial.blogspot.com/2013/11/union-in-mysql.html
You are doing the union before and then applying the where clause. So you would get a unique combination of "requester_name,receiver_name" and then the where clause would apply. Apply the where clause in each select...
Select requester_name,receiver_name from poem_authors_follow_requests
where (LOWER(requester_name)=LOWER('user1')
or LOWER(receiver_name)=LOWER('user1'))
UNION
Select requester_name,receiver_name from poem_authors_friend_requests
where (LOWER(requester_name)=LOWER('user1')
or LOWER(receiver_name)=LOWER('user1'))
In your where statement, reference the alias "u" for each field refence in your where statement.
So the beginning of your where statement would be like: where (LOWER(u.requester_name) = ...
This is simlar to the answer you can see in: WHERE statement after a UNION in SQL?
You should be able to use the INTERSECT keyword instead of doing a nested query on a UNION.
SELECT member_id, name FROM a
INTERSECT
SELECT member_id, name FROM b
can simply be rewritten to
SELECT a.member_id, a.name
FROM a INNER JOIN b
USING (member_id, name)
http://www.bitbybit.dk/carsten/blog/?p=71
1000 Apologies if I've repeated a question, couldn't find an answer here to my question.
I'm try to retrieve the data from 2 separate columns from 2 unrelated tables in the same query.
I've tried using a UNION statement, but the problem is that I need to be able to separate the results into 'venues' and 'programmes' - here was what I did:
SELECT venue_name
FROM my_venues
UNION
SELECT programme_title
FROM my_programmes;
Maybe it's not necessary to combine the query and I can just do 2 separate queries? The database won't be especially large, but it seems unnecessary...
Help and thanks!
Just add a constant column in both selects, with the same name, but different values:
SELECT "venues" as source, venue_name as thing_name
FROM my_venues
UNION ALL
SELECT "programmes" as source, programme_title as thing_name
FROM my_programmes;
Now:
Rows with value "venues" for column
source will come from the table
my_venues ,
rows with value "programmes" for
column source will come from table
my_programmes.