I have 2 tables in MySQL t1 and t2. Both have same column names. Table t1 has huge data and t2 is not so huge as compared to t1 but in mean time t2 would also be of the same size as t1. The only difference is that the id column do not match in both the tables. I want to create a view out of these column.
What I have created is
CREATE VIEW vw_t1t2 AS SELECT id , name , lastname, depid FROM t1
Union
SELECT id , name , lastname, depid FROM t2;
If I do a query "Select * from vw_t1t2 where depid='100287'".
The view does not fetch the correct data, the data is mixture on all the records when I search for particular department id, some records are of different department id. Also it took 200 sec to execute the query.
you may consider to wrap the result of the union
SELECT * FROM
(
SELECT id , name , lastname, depid FROM t1
Union
SELECT id , name , lastname, depid FROM t2
) as sel1
WHERE depid = 2
but let us see the EXPLAIN output to optimize the query
Related
table1: table2
studentname StudentAge
stuart 18
I have following queries like
1)
select studentname as sname from table1;
sname
Stuart
2)
select StudentAge as age from table2;
age
18
I want to display the query results in a table having two columns as:
category Values
sname Stuart
age 18
If your tables have at most one row, you can use subqueries:
select (select studentname from table1) as sname
(select StudentAge from table2) as age
Given the data you've presented, with only a single row in each table, you can use a CROSS JOIN to join them:
select t1.studentname, t2.StudentAge
from table1 t1
cross join table2 t2
This will perform a Cartesian join, where each row in each table is joined to every row in the other table, and the number of returned rows is the product of the number of rows in the two tables.
db<>fiddle here
This suggests me union all :
select t.category, t.values
from ( (select t.studentname as values, 'studentname' as category from table1 t) union all
(select t.stuedentage, 'age' as category from table2 t)
) t;
I have an SQL database with several tables of patient data. Every table has one column in common, an ID number representing each patient. There is significant overlap between the tables, i.e. the same patient ID number often appears on multiple tables. What I would like to do is SELECT all distinct patient ID numbers that do not appear on one specific table.
You can use UNION and NOT IN like this:
select id
from (
select id from table1
union
select id from table2
union
select id from table3
...
) t where id not in (
select id from sometable
);
I am having problem to find records from two tables where both the tables have common name field and both fields have comma separated values.
For instance,
table-1 have "a,b,c" value
id | name
----------
1 | a,b,c
and table-2 have "a,c,d,e,f" value
id | name
---------------
1 | a,c,d,e,f
Now I want to compare both tables where at least one character matches in both.
So is it possible to get records where at least one character matches into both fields or not?
Thanks in advance :)
First, use the UNION statement to combine rows in both tables; include only the columns that need to compare. The returned result set is used for the comparison. Considering table-1 as t1 and table-2 as t2.
SELECT t1.pk, t1.c1
FROM t1
UNION ALL
SELECT t2.pk, t2.c1
FROM t2
Second, group the records based on the primary key and columns that need to compare. If the values in the columns that need to compare are identical, the COUNT() returns 2, otherwise the COUNT() returns 1.
See the following query:
SELECT pk, c1
FROM
(
SELECT t1.pk, t1.c1
FROM t1
UNION ALL
SELECT t2.pk, t2.c1
FROM t2
) t
GROUP BY pk, c1
HAVING COUNT(*) = 1
ORDER BY pk
MySQL compare two tables example
SELECT id,title
FROM (
SELECT id, title FROM t1
UNION ALL
SELECT id,title FROM t2
) tbl
GROUP BY id, title
HAVING count(*) = 1
ORDER BY id;
It will return unmatched records.
I have four tables with same fields. Now I want to join these tables in such a way that I retrieve records only if there is a match between any two tables on a field(like name).
Thanks in advance.
This would return all the name values that appear in more than one table:
select
name
from
(select distinct
name
from table1
union all
select distinct
name
from table2
union all
select distinct
name
from table3
union all
select distinct
name
from table4) temp
group by name
having count(*) > 1;
Check out the interactive example.
I have two tables with identical schema. I want to get a count of all the people with a given surname in both tables, and have found I can do it like this:
SELECT surname, count(*) AS cnt
FROM
(
SELECT surname
FROM people.NorthKorea
UNION ALL
SELECT surname
FROM peopleGlobal.NorthKorea
) AS t
GROUP BY surname
ORDER BY cnt DESC
This is fine for small tables, but I have tables with up to 250 million rows, so was wondering if there may be a more efficient way of doing this? Such as INSERTING the result of the COUNT from one table into a table, and then updating / inserting (REPLACE?) the result of the COUNT on the second table.
N.B. I actually want to store the result of the COUNT on both tables in another table.
An index on the surname column should help a lot. I would try with this query, if there are a lot more rows than surnames I expect it to run faster:
SELECT surname, SUM(cnt)
FROM
(
SELECT surname, COUNT(*) as cnt
FROM people.NorthKorea
GROUP BY surname
UNION ALL
SELECT surname, COUNT(*) as cnt
FROM peopleGlobal.NorthKorea
GROUP BY surname
)
GROUP BY surname
ORDER BY cnt DESC