SELECT breakgame, Streak,
((SELECT (maxGameId - gameId) as gameGap
FROM game_result
WHERE game_result.breakgame >= kokopam.game_streak.breakgame
ORDER BY gameId DESC LIMIT 1)/ Streak) as nowWeight
FROM kokopam.game_streak, (SELECT max(gameId) as maxGameId FROM game_result ORDER BY gameId DESC LIMIT 1) maxGameId
WHERE breakgame>= 2
how to change this query to use join?
please help me
In first place, you should've a condition in the "where" clause that states the ID that the rows share.
Anyway, the method you're using works the same as inner join.
Select *
From tableA a, tableB b
Where a.id=b.id
Is the same as
Select *
From tableA a
Inner join tableB b on b.id=a.id
I could help you a bit more if you specify what you were trying do in the query and the columns that the tables have.
Related
I need SQL Query i mention details in sample image, I hope you understand
Thank you
Try this query and let us know if it works for you:-
Select a.*, b.degree from table_a as a inner join table_b as b on
b.log_id=a.log_id order by a.log_id asc
In this database query we have performed an inner join between the two tables and matched it based on log_id and at last order it in ascending order based on log_id itself.
This should work:
SELECT a.*, b.degree
FROM a
LEFT JOIN b ON a.s_id = b.n_id
WHERE a.log_id = 102
ORDER BY a.log_id ASC;
Remember to change the table names a and b to the correct table names. More on this topic here.
This code works:
Select a.*, b.degree from table_a as a inner join table_b as b on b.log_id=a.log_id where a.log_id="102" order by a.log_id asc;
Thanks you all
If I have the following two tables:
Table "a" with 2 columns: id (int) [Primary Index], column1 [Indexed]
Table "b" with 3 columns: id_table_a (int),condition1 (int),condition2 (int) [all columns as Primary Index]
I can run the following query to select rows from Table a where Table b condition1 is 1
SELECT a.id FROM a WHERE EXISTS (SELECT 1 FROM b WHERE b.id_table_a=a.id && condition1=1 LIMIT 1) ORDER BY a.column1 LIMIT 50
With a couple hundred million rows in both tables this query is very slow. If I do:
SELECT a.id FROM a INNER JOIN b ON a.id=b.id_table_a && b.condition1=1 ORDER BY a.column1 LIMIT 50
It is pretty much instant but if there are multiple matching rows in table b that match id_table_a then duplicates are returned. If I do a SELECT DISTINCT or GROUP BY a.id to remove duplicates the query becomes extremely slow.
Here is an SQLFiddle showing the example queries: http://sqlfiddle.com/#!9/35eb9e/10
Is there a way to make a join without duplicates fast in this case?
*Edited to show that INNER instead of LEFT join didn't make much of a difference
*Edited to show moving condition to join did not make much of a difference
*Edited to add LIMIT
*Edited to add ORDER BY
You can try with inner join and distinct
SELECT distinct a.id
FROM a INNER JOIN b ON a.id=b.id_table_a AND b.condition1=1
but using distinct on select * be sure you don't distinct id that return wrong result in this case use
SELECT distinct col1, col2, col3 ....
FROM a INNER JOIN b ON a.id=b.id_table_a AND b.condition1=1
You could also add a composite index with use also condtition1 eg: key(id, condition1)
if you can you could also perform a
ANALYZE TABLE table_name;
on both the table ..
and another technique is try to reverting the lead table
SELECT distinct a.id
FROM b INNER JOIN a ON a.id=b.id_table_a AND b.condition1=1
Using the most selective table for lead the query
Using this seem different the use of index http://sqlfiddle.com/#!9/35eb9e/15 (the last add a using where)
# USING DISTINCT TO REMOVE DUPLICATES without col and order
EXPLAIN
SELECT DISTINCT a.id
FROM a
INNER JOIN b ON a.id=b.id_table_a AND b.condition1=1
;
It looks like I found the answer.
SELECT a.id FROM a
INNER JOIN b ON
b.id_table_a=a.id &&
b.condition1=1 &&
b.condition2=(select b.condition2 from b WHERE b.id_table_a=a.id && b.condition1=1 LIMIT 1)
ORDER BY a.column1
LIMIT 5;
I don't know if there is a flaw in this or not, please let me know if so. If anyone has a way to compress this somehow I will gladly accept your answer.
SELECT id FROM a INNER JOIN b ON a.id=b.id_table_a AND b.condition1=1
Take the condition into the ON clause of the join, that way the index of table b can get used to filter. Also use INNER JOIN over LEFT JOIN
Then you should have less results which have to be grouped.
Wrap the fast version in a query that handles de-duping and limit:
SELECT DISTINCT * FROM (
SELECT a.id
FROM a
JOIN b ON a.id = b.id_table_a && b.condition1 = 1
) x
ORDER BY column1
LIMIT 50
We know the inner query is fast. The de-duping and ordering has to happen somewhere. This way it happens on the smallest rowset possible.
See SQLFiddle.
Option 2:
Try the following:
Create indexes as follows:
create index a_id_column1 on a(id, column1)
create index b_id_table_a_condition1 on b(a_table_a, condition1)
These are covering indexes - ones that contain all the columns you need for the query, which in turn means that index-only access to data can achieve the result.
Then try this:
SELECT * FROM (
SELECT a.id, MIN(a.column1) column1
FROM a
JOIN b ON a.id = b.id_table_a
AND b.condition1 = 1
GROUP BY a.id) x
ORDER BY column1
LIMIT 50
Use your fast query in a subselect and remove the duplicates in the outer select:
SELECT DISTINCT sub.id
FROM (
SELECT a.id
FROM a
INNER JOIN b ON a.id=b.id_table_a && b.condition1=1
WHERE b.id_table_a > :offset
ORDER BY a.column1
LIMIT 50
) sub
Because of removing duplicates you might get less than 50 rows. Just repeat the query until you get anough rows. Start with :offset = 0. Use the last ID from last result as :offset in the following queries.
If you know your statistics, you can also use two limits. The limit in the inner query should be high enough to return 50 distinct rows with a probability which is high enough for you.
SELECT DISTINCT sub.id
FROM (
SELECT a.id
FROM a
INNER JOIN b ON a.id=b.id_table_a && b.condition1=1
ORDER BY a.column1
LIMIT 1000
) sub
LIMIT 50
For example: If you have an average of 10 duplicates per ID, LIMIT 1000 in the inner query will return an average of 100 distinct rows. Its very unlikely that you get less than 50 rows.
If the condition2 column is a boolean, you know that you can have a maximum of two duplicates. In this case LIMIT 100 in the inner query would be enough.
If I run an inline select subquery, I can filter the rows to line up with the root query. E.G.
A.Field1, A.Field2, (SELECT B.Field1 FROM tblB as B WHERE B.AID = A.ID ORDER BY B.DateAdded LIMIT 1)
FROM tblA as A
But If I try to move that select subquery to a joined subquery I can't use the where criteria (WHERE B.AID = A.ID) and there's no way to limit tblB fields to only matching tblA's row.
So what is the correct way to modify the query so I can select B.Field1, B.Field2, etc. when dealing with a 1:M?
sqlfiddle
I have written a mysql query to retrieve data from two tables with subquery and join.
The query works fine but I want to avoid subquery because of some performance issues.
It should be ordered by date time before group.
SELECT a.value1, a.value2, b.value1 FROM (SELECT * FROM A ORDER BY datetime DESC, id DESC) AS a
INNER JOIN B AS b ON b.a_id=a.id
WHERE a.value4="value"
GROUP BY b.value2, b.value3;
I have tried several ways to rewrite this without a subquery but still couldn't find a solution.
Is it possible to avoid a sub-query in this case?
There appears to be no purpose to your subquery in the first place. All you are doing is performing an ORDER BY, which will be lost the instant you join to the other table. You should be able to do this:
SELECT a.value1, a.value2, b.value1 FROM
A as a
INNER JOIN
B AS b
ON b.a_id=a.id
WHERE a.value4="value"
GROUP BY b.value2, b.value3;
You haven't got any filter on A table, so you can directly join the table:
SELECT b.value2, b.value3 FROM A AS a
INNER JOIN B AS b ON b.a_id=a.id
WHERE a.value4='value'
GROUP BY b.value2, b.value3;
Note you have to select columns that were grouped by.
You can try this
SELECT a.value1, a.value2, b.value1 FROM A AS a INNER JOIN B AS b ON b.a_id=a.id WHERE a.value4="value" GROUP BY b.value2, b.value3 ORDER BY a.datetime DESC, a.id DESC;;
I've 3 tables say A,B,C.
Table A has userid column.
Table B has caid column.
Table C has lisid and image columns.
one userid can have one or several caids.
one caid can have one or several lisids.
how do I select a userid which has maximum number of rows with image column as not null (in some lisids image column is blank and in some it has some value).
can someone please help.
Presumably, the ids are spread among the tables in a reasonable fashion. If so, the following should do this:
select b.userid, count(*)
from TableB b join
TableC c
on b.caid = c.caid
where c.image is not null
group by b.userid
order by count(*) desc
limit 1
The question in the comments is how you connect TableA to TableB and TableB to TableC. The reasonable approach is to have the userid in TableB and the caid in TableC.
Getting all the rows with the max requires a bit more work. Essentially, you have to join in the above query to get the list
select s.*
from (select b.userid, count(*) as cnt
from TableB b join
TableC c
on b.caid = c.caid
) s
(select count(*) as maxcnt
from TableB b join
TableC c
on b.caid = c.caid
group by b.userid
order by count(*) desc
limit 1
) smax
on s.cnt = smax.cnt
Other databses have a set of functions called window functions/ranking functions that make this sort of query much simpler. Alas, MySQL does not offer these.