(SELECT a FROM Table 1 WHERE a=10 AND B=2 LIMIT 1000)
UNION ALL
(SELECT a FROM Table 2 WHERE a=10 AND B=2 LIMIT 1000)
UNION ALL
(SELECT a FROM Table 3 WHERE a=10 AND B=2 LIMIT 1000)
Expecting:- instead of entering 3 times values in t1, t2, t3 tables
I want to fetch the data from 3 tables, where a=10 and b=2. But instead of entering data 3 Times separately in each table, I want it to enter only once. So can we do that
Thank you in advance
I'm not exactly sure what you're asking for, but something like an inner join might be what you need:
SELECT t1.a, t2.a, t3.a
FROM t1
INNER JOIN t2
ON t1.b = t2.b
INNER JOIN t3
ON t2.b = t3.b;
I'm assuming that a and b are referring to the same things in all 3 tables.
Related
I have read here that MySQL processes ordering before applying limits. However, I receive different results when applying a LIMIT parameter in conjunction with a JOIN subquery. Here is my query:
SELECT
t1.id,
(t2.counts / c.matches)
FROM
table_one t1
JOIN
table_two t2 ON t1.id = t2.id
JOIN
(
SELECT
t1.id, COUNT(DISTINCT t1.id) AS matches
FROM
table_one t1
JOIN table_two t2 ON t1.id = t2.id
WHERE
t1.id IN (3390 , 3236, 148, 2811, 829, 137)
AND t2.value_one <= 30
AND t2.value_two < 2
GROUP BY t1.id
ORDER BY (t2.counts / matches)
LIMIT 0, 50 -- PROBLEM IS HERE (I think)
) c ON c.id = t1.id
ORDER BY (t2.counts / c.matches), t1.id;
Here is a rough description of what I think is happening:
The sub-query selects a bunch of ids from table_one that meet the criteria
These are ordered by (t2.counts / matches)
The top 50 (in ascending order) are fashioned into a table
This resulting table is then joined on the the id column
Results are returned from the top level JOIN - without a GROUP BY clause this time. table_one is a reference table so this will return many rows with the same ID.
I appreciate that some of these joins don't make a lot of sense but I have stripped down my query for readability - it's normally quite chunky .
The problem is that when, I include the LIMIT parameter I get a different set of results and not just the top 50. What I want to do is get the top results from the subquery and use these to join onto a bunch of other tables based on the reference table.
Here is what I have tried so far:
LIMIT on the outer query (this is undesirable as this cuts off important information).
Trying different LIMIT tables and values.
Any idea what is going wrong, or what else I could try?
I have found a solution to my problem. It seems as if my matches column name does can't be used in my ORDER BY clause - which is weird since I don't get an error. Either way, this solves the problem:
SELECT
t1.id,
(t2.counts / c.matches)
FROM
table_one t1
JOIN
table_two t2 ON t1.id = t2.id
JOIN
(
SELECT
t1.id, COUNT(DISTINCT t1.id) AS matches
FROM
table_one t1
JOIN table_two t2 ON t1.id = t2.id
WHERE
t1.id IN (3390 , 3236, 148, 2811, 829, 137)
AND t2.value_one <= 30
AND t2.value_two < 2
GROUP BY t1.id
ORDER BY (t2.counts / COUNT(DISTINCT t1.id)) -- This line is changed
LIMIT 0, 50
) c ON c.id = t1.id
ORDER BY (t2.counts / c.matches), t1.id;
I want to get value from table1 and join all matching value from table2. The table1 has to be limited to 2 rows, but expecting output should own all matching values for those two ids.
How can I achieve this?
You would use a subquery:
select t1.*, t2.*
from (select t1.*
from table1 t1
limit 10
) t1 left join
table2 t2
on t1.id = t2.table1_id;
Note: This returns two arbitrary rows. Normally, you would have an order by to better specify the rows. And use order by rand() for random rows.
if you want all the value in join for only two row of table 1 you can use a subqiuery with limit 2
select b.id, a.value, b.value2, b.table1_ID
from (
select * from table1
limit 2
) a
inner join table2 on aid = b.table1_ID
I have two tables from which I need to get data in the same SELECT output. The thing is that I need to limit the amount of results.
Say I have an ID column that is unique in table1, but in table2 it has many rows with that ID.
Now I just want to list how many different IDs I have in table1 and some other information stored in table2.
How can I get the desired output I show in the end?
To make my idea clear I used a "messenger" database for an example.
Tables
T1
Id_thread Date
1 13Dic
2 12Dic
T2
Id_thread Message Name
1 Hi Someone
1 Hi to you Someone
2 Help me? Someother
2 Yes! Someother
Desired output
T1.Id_thread T2.Name T1.Date
1 Someone 13Dic
2 Someother 12Dic
I'd join and use distinct:
SELECT DISTINCT t1.id_thread, t2.name, t1.date
FROM t1
JOIN t2 ON t1.id_thred = t2.id_thread
Use a JOIN and GROUP BY:
SELECT t1.Id_thread, t2.Name, t1.Date
FROM t1
JOIN t2 ON t1.Id_thread = t2.Id_thread
GROUP BY t1.Id_thread
Note that if Name is the same for all rows in t2 that have the same Id_thread, that column probably should be in t1. If you fix that, you don't need the JOIN.
Try this:
SELECT DISTINCT T1.Id_thread, T2.Name, T1.Date
FROM T1
LEFT OUTER JOIN T2 ON T1.Id_thread = T2.Id_thread
select T1.Id_thread,T2.Name,T1.Date from T1
inner join T2 on T1.Id_thread = T2.Id_thread
group by T1.Id_thread
order by T1.Id_thread
You haven't specified how you want the limit the results from Table 2. Considering you just want one row, you can use a CROSS APPLY:
Select T1.Id_thread,T2Table.Name,T1.Date From T1
Cross Apply (Select Top 1 T2.Name From T2 Where T2.Id_thread=T1.Id_thread) T2Table
You can specify other conditions in the inner Select statement if you wish.
The following query is the one I was using previously... but I want to combine these two queries in order to improve the performance
select a, b, c
from table1
where d LIKE 'xxx'
and f like 'yyyy'
order by b desc;
I'm executing the above query and reading values.
For every value of b from above query again executing the below query in a loop.
select count(*)
from table2 where b=? AND js_email_id IN
(
select js_email_id
from js_initialsignup
where UCase(jsaccountstatus) LIKE UCase('live')
AND UCase(js_status) LIKEUCase('accepted')
)
How can I combine both queries and get count and values at a time?
select a,b,c,
(select count(*)
from table2 where b=a.b AND js_email_id IN
(
select js_email_id
from js_initialsignup
where UCase(jsaccountstatus) LIKE UCase('live')
AND UCase(js_status) LIKEUCase('accepted')
)) as cnt
from table1 a
Try this:
SELECT t1.a, t1.b, t1.c, COUNT(t2.*)
FROM table1 t1
INNER JOIN table2 t2 ON t1.b = t2.b
INNER JOIN js_initialsignup j ON t2.js_email_id = j.js_email_id
WHERE t1.d LIKE 'xxx'
AND t1.f like 'yyyy'
AND UCase(j.jsaccountstatus) LIKE UCase('live')
AND UCase(j.js_status) LIKE UCase('accepted'))"
GROUP BY t1.a, t1.b, t1.c
ORDER BY by t1.b DESC;
Is there any major difference from an optimisation point of view between the following two alternatives? In the first option I alias the table, so the total_paid calculation is only run once. In the second option, there is no table alias, but the SUM calculation is required a few times.
Option 1
SELECT tt.*, tt.amount - tt.total_paid as outstanding
FROM
(
SELECT t1.id, t1.amount, SUM(t2.paid) as total_paid
FROM table1 t1
LEFT JOIN table2 t2 on t1.id = t2.t1_id
GROUP BY t1.id
) as temp_table tt
WHERE (tt.amount - tt.total_paid) > 0
LIMIT 0, 25
Option 2
SELECT t1.id, t1.amount, SUM(t2.paid) as total_paid
, (t1.amount - SUM(t2.paid)) as outstanding
FROM table1 t1
LEFT JOIN table2 t2 on t1.id = t2.t1_id
WHERE (t1.amount - SUM(t2.paid)) > 0
GROUP BY t1.id
LIMIT 0, 25
Or perhaps there is an even better option?
if you run the queries with EXPLAIN, you'll be able to see what's going on 'inside'.
Also, why don't you just run it and compare the execution times?
Read more on explain here