mySQL select columns from different tables - mysql

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.

Related

Get a multiple UNION ALL query faster

I have an issue of performance with a query with multiple UNION ALL statements. I need to add (row by row) data from different tables into the same columns. The query need to be used to create a view in MySQL, so, here an example:
CREATE OR REPLACE
ALGORITHM = UNDEFINED
DEFINER = usr
SQL SECURITY DEFINER
VIEW my_view AS
SELECT DISTINCT
column 1,
column 2,
column 3
FROM
table 1
WHERE
condition 1
UNION ALL
SELECT DISTINCT
column 1,
column 2,
column 3
FROM
table 2
WHERE
condition 2
UNION ALL
SELECT DISTINCT
column 1,
column 2,
column 3
FROM
table 3
WHERE
condition 3
It seems pointless to do all the multiple UNION ALLs just to add (row by row) data from the same features (not just 3 columns as in the example, I have many more) coming from different tables because this is something that requires lots of resources from the DB, leading to "lost connection error during the query" due to the time it takes to run.
Is there any way to optimize this kind of query?
Thanks in advance.
UNION ALL is the most performant way of concatenating result sets. (UNION is slower because it removes duplicates.)
Surely your timeout occurs when you use the view, not when you create it.
Your performance issue stems from one or more of the SELECT queries in your UNION ALL cascade being very slow. You, or your "data engineer" colleagues, may need to create appropriate indexes on your table 1, table 2, table 3 tables.
To figure this out, do these things.
Read Optimizing Queries With EXPLAIN.
Run SHOW CREATE TABLE whateverTableName;. Look at the output. It will show you the indexes.
Run the SELECT queries using that same table prefixed with EXPLAIN. It will show you the indexes it used to satisfy the query.
Ask another question here showing us the output from those two steps.
Or, it's possible your resultset from your big query is vast. There's no magic that can process millions of rows faster than O(n).

MYSQL Union between two large tables

This is probably a simple question for you gurs out there, but my limited knowledge about MYSQL is really showing here:
I have two tables:
Table Q with fields id(pk), symbol, timestamp(bigint) and a few data fields
Table T with fields id(pk), symbol, timestamp(bigint) and a few data fields.
Table Q has about 800 million rows, table T about 80 million rows.
I want a report for one symbol, where rows from Q and T are mixed in timestamp order. With a row from T, the data fields from Q should be NULL and vice versa.
Can someone please recommend how the query should look? Also, a recommendation on how the index should be constructed would be great.
Have tried a lot of variations on inner, outer joins, union all etc but to no avail.
It sounds like you need a simple UNION ALL (could be union since you indicate that an entry will only exist in only one table or the other anyhow). Since the tables are the same, should be easy.
I would suggest an index on the table based on your criteria PLUS the date/time field if you want the transactions in a specific order, otherwise an ORDER BY clause can kill your time performance. If you want more columns, just make sure that each query of the union matches the same order of columns desired and same data types too.
select ID, Symbol, timestamp
from Q
where symbol = 'something'
UNION ALL
select ID, Symbol, timestamp
from T
where symbol = 'something'
Again, if you wanted more criteria, you could adjust for each WHERE clause to something like
where symbol = 'something'
and timestamp between someStartTime and someEndTime

How to find a column name in all tables of MS Access database?

I am working with a large MS Access Database with several tables. I am wondering how could I search a column from all tables in the database?
A UNION query would be one way to do it.
SELECT FieldA from Table 1
UNION
Select FieldA from Table 2
UNION
Select FieldA from Table 3
Save the above as a query and then you could write another query that contains the first with whatever criteria you like.
I know this seems tedious, but that's the problem with having your data spread out so much instead of pulling it into a single table. If you can provide details on exactly what you are trying to do maybe we can give a more specific answer.

mysql query two tables, UNION and where clause

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

MySQL: Merge two tables (same schema) and create a new one - SINGLE QUERY

I have two tables with exactly the same schema.
I would like to have a 3rd table, containing all the data of these two tables combined.
How can I do this with an INSERT INTO query (single query doing this)?
I know I can do this with: INSERT INTOname_of_new_tableSELECT DISTINCT * FROMname_of_old_table but then I would need to do it twice. I am seeking to doing this with a SINGLE query.
Thank you.
INSERT INTO new_table
SELECT * FROM old_table_1
UNION
SELECT * FROM old_table_2
Removed your DISTINCT, as UNION has an implied DISTINCT (UNION ALL does not have this). This will only work if the tables are identical in column count and have similar column type, however. Then again, it sounds like that's your situation.