Joining two similar tables in MySQL - sql

I have two tables with similar columns - let's say table A with column LABEL_A
and table B with column LABEL_B. The data types of LABEL_A and LABEL_B are same.
How can I select LABELs from both tables in a single query? (So the the result of the query contains single column LABEL containing data from LABEL columns of both tables).
EDIT: Can I use such UNION queries in a CURSOR?
Thanks for answers.

Use:
SELECT a.label_a
FROM TABLE_A a
UNION ALL
SELECT b.label_b
FROM TABLE_B b
UNION ALL will be faster, but won't remove duplicates if they exist. Use UNION if you want duplicates removed.

Use a UNION:
select LABEL_A as Label from A
union
select LABEL_B as Label from B

Related

How to merge data of two tables with different column name in Big Query

How can I get final output based on table 1 and table 2 in Big Query
Table 1
Table 2
Final Output
You can use union all. If the columns are in the same order:
select *
from table1
union all
select *
from table2;
In general, though, it is better to list out the column names instead of using *. Note that in the result set, the names from the first select are used for the result set.

How to Identify matching records in two tables?

I have two tables with same column names. There are a total 40 columns in each table. Both the tables have same unique IDs. If I perform an inner join on the ID columns I get a match on 80% of the data. However, I would like to see if this match has exactly same data in each of the columns.
If there were a few rows like say 50-100 I could have performed a simple union operation ordered by ID and manually checked for the data. But both the tables contain more than 5000 records.
Is a join on each of the columns a valid solution for this or do I need to perform concatenation?
Suppose you have N columns, you can add GROUP BY COL1,COL2,....COLN
select * from table1
union all
select * from table2
group by COL1, COL2, ... , COLN
having count(*)>1;
Reference: link

SQL How can I select data that appear stored in similar columns from two tables ignoring the case?

For example I have two table A and B which both has a column people. And I want to select those people that appears in both table.
However the name in table A is in Upper case while those in B only has the first letter in Upper case.
For example, there is a BOB in table A and a Bob in table B, and I want Bob to be selected in my result.
Convert the name to the same case and do an intersect
to find people that appear in both tables
select upper(people)
from TableA
intersect
select upper(people)
from TableB
order by 1
One method is join:
select distinct a.name
from tablea a join
tableb b
on lower(a.name) = lower(b.name);
This is not necessarily efficient, because of the case conversion and distinct. Some databases, by the way, are case-insensitive by default.

Combine results of two table in SQL

i have two tables one contain computer_science_book and Mechanical_engineering_book i want the result which contail all books of computer_science_book and Mechanical_engineering_book .
give me suggetions.
You can use union all for that:
select * from computer_science_book
union all
select * from Mechanical_engineering_book
You can use the UNION operator:
select columns from computer_science_books
UNION
select columns from mechanical_eng_books
Note that the structure of the results from computer_science_books needs to match the structure of results from mechanical_eng_books. The reason is that, as asked for, there is only one result table. There is also the UNION ALL operator. The difference is that UNION removes duplicates whereas UNION ALL returns all combined rows.

Query multiple tables in access

We have 50 tables we need to query a column that exists in all. This column is a checkbox. We need to count per table how many are checked and how many are unchecked. Cant seem to get 1 query to count results and display per table as opposed to multiplying or combining results.
We need 1 column per table to display count of checked and unchecked.
Thanks
SELECT "Table1" , Count('qcpass') AS column
FROM 5000028
GROUP BY [5000028].qcpass
union
SELECT "Table2",count('qcpass')
FROM 5000029
Group By [5000029].qcpass;
Edit
Based on your feedback, try this (sorry, didn't realize you wanted 1 column per table):
Make a union query that combines all 50 tables. The result should be 1 row per table:
SELECT "5000028" as QCPASS, Count () FROM 5000028 group by QCPASS
UNION
SELECT "5000029" as QCPASS, Count () FROM 5000029 group by QCPASS
UNION...
Now make a "Crosstab" query which is pretty easy in Access. First, make a new query and select the Crosstab option at the top. This query will use the union query as its source.
This will have 3 columns. The first will be a constant value (you can use "Totals" if you like, it's just a placeholder). Set this as your "Row Heading".
The 2nd column will be QCPass. Set this as your "Column Heading".
The 3rd column will be Expr1. Set this as your "Value".
When you run this, you should see a 1-row table with 1 column per each of your source tables.
SELECT columna, 'tablename1' from tablename1 where ..
UNION
SELECT columna, 'tablename2' from tablename2 where ..
UNION
SELECT columna, 'tablename3' from tablename3 where ..
...
SELECT columna, 'tablename4' from tablename50 where ..