Avoid sub query - mysql

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;;

Related

MySql SELECT * FROM Table A and JOIN Column FROM Table B WHERE LOG_ID Match

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

Why 'select * from Employee group by Id' is much faster than directly use the Employee table?

In LeetCode Database problems, one called 'Employees Earning More Than Their Managers'. the link is here: https://leetcode.com/problems/employees-earning-more-than-their-managers/description/. The logic is simple:
select t.Name
from Employee t
join Employee t1
on t.ManagerId = t1.id
where t.Salary>t1.Salary
This costs 350ms. But then I find one fast submission:
select a.Name as Employee from
(
select * from Employee
group by Id
) a
left join
(
select * from Employee
group by Id
) b on a.ManagerId = b.Id
where a.Salary > b.Salary
which costs 240ms. The crux may lie in select * from Employee group by Id. So I wonder why select * from table group by id can make the query faster. Thanks.
On first query you compare
on t.gerId = t1.id
On second
on a.ManagerId = b.Id
Also on first is a inner join on second a left join, those queries and results are not the same.

how to change this query to use join?

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.

How to to use both indexes for ORDER BY and GROUP BY clause?

I have the following MySQL tables article and category and category_article.Every article may belongs to more than category and that is my SQL query.
SELECT * FROM article a
LEFT JOIN category_article ca ON a.id=ca.aid
LEFT JOIN category c ON c.id=ca.cid
GROUP BY a.id
ORDER BY a.date DESC
WHERE c.status='1'
LIMIT 0,10
Using different columns for GROUP BY and ORDER BY prevents indexes from using.
So is there another technique that i can use for speeding up this query.
As you are not doing any aggregation you could probably leave out the group by and just order the results by a.ID, a.date and get the same results. This should result in a faster query execution plan.
SELECT * FROM article a
Left JOIN category_article ca ON a.id=ca.aid
Left JOIN category c ON c.id=ca.cid
ORDER BY a.id, a.date DESC

MySQL query help on joins and counting most occurance

I have two tables, client_table and order_table
Entries in the client_table are unique with id as primary key
In the order_table, the order_id is the primary key and also a client_id field. The client_id is the primary key of the client_table.
There can be multiple entries in the order_table that contain the same client_id. So it is a one-to-many relationship.
I'm trying to come up with a query that produce the most occurring client_id in the order_table
I have tried the following queries.
SELECT a.id FROM `client_table` as a inner join order_table as b GROUP BY a.id ORDER BY count(b.client_id)
So I'm looking for a result of client_id which have the most orders. I suppose I only need the order_table and don't need the client_table at all right ?
SELECT b.client_id, count(*) AS Clients
FROM order_table AS b
GROUP BY b.client_id
ORDER BY Clients DESC
No need for a join if you only want the clients id, like you said. Also, if you want to join, like in your example query, you need to specify on which column with a ON clause.
... inner join order_table as b on a.id=b.client_id ...
Yes, you don't need the client table at all:
select
client_id,
count(order_id) order_count
from
order_table
group by
client_id
order by
count(order_id) DESC
you can try:
SELECT a.id FROM `client_table` as a left join order_table as b ON a.id = b.client_id GROUP BY a.id ORDER BY count(b.client_id) desc
Use DESC at ORDER BY to get most occurances. Right now you only get the lowest occurances, and try changing INNER JOIN to LEFT JOIN.