What is most Efficient SQL Query? [closed] - mysql

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to retrieve all the 'events' of a user (given user_id) using his subscriptions.
I know this can be done using subquery but it is slow.
Please suggest the best approach to achieve it.
Here is the sqlfiddle link:
http://sqlfiddle.com/#!2/dd48e3
And here is the design image:

If u remove the where clause you will get the events corresponding to each user
select e.*,s.fname
from users s
join routine r ON (s.user_id = r.user_id)
join events e ON (e.r_id = r.r_id)
where s.user_id = ?

select * from events e left join subscriptions s on s.r_id = e.r_id where s.user_id = ?

Related

How to select specific columns from different tables in mysql database in php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have 3 tables:
request(RequestID,Name,Mobie Number,Vehicle Number,Location)
reqresponse(RequestID,ResponseID,MechanicID,MechanicName)
mechdetails(MechanicID,MechanicName,MechanicMobile,MechanicAvalability)
Now, what I want to do is select all data from the request table and display it along with details of the mechdetails table, where MechanicAvailability is set to 'busy'.
RequestID is the foreign key in reqresponse table.
Can anyone please tell me the MySQL code to do so.
I am working in PHP.
This is where I am right now:
SELECT * FROM mechdetails WHERE MechAvailability='Busy' AND
MechanicID=(SELECT MechanicID FROM reqresponse WHERE ResponseID='$rid')
I am really new to MySQL and PHP so please help me!
The query
SELECT * FROM request r
JOIN reqresponse q ON q.RequestID = r.RequestID
JOIN mechdetails m ON m.MechanicID = q.MechanicID
WHERE MechanicAvailability = 'busy'
should do the job.
To access the resulting data, use mysqli_query and mysqli_result.
You can join the tables:
select r.*, m.*
from request r
inner join reqresponse rr on rr.requestid = r.requestid
inner join mechdetails md on md.mechanicid = rr.mechanicid
where md.mechanicavailability = 'busy'

mySQL select every record that does not exist in another table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have 3 tables:
users (id_user)
achievements (id_achievement)
user_achievements (id_user, id_achievement)
I need help selecting every achievement that a user has not yet completed.
Consider a cross join that generates all combinations of users and achievements, and a not exists condition with a correlated subquery to filter out those that were completed already:
select u.id_user, a.id_achievement
from users u
cross join achievements a
where not exists (
select 1
from user_achievements ua
where ua.id_user = u.id_user and ua.id_achievement = a.id_achievement
)

Show in the tables, only the last statues [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a simple entry in the oracle, there are 3 columns, date, API name and status. I want that in the answer I had not all history, and only the last on each of Names of API (only 7). I will be grateful for your help. I know that asked of the very difficult but I'm just new to oracle.
select l.log_date,l.job_name,l.status from user_scheduler_job_log l
could be you want the related status for name and last log_date
select u.job_name, u.status, t.max_date
from user_scheduler_job_log u
INNER JOIN(
select MAX(l.log_date) max_date, l.job_name
from user_scheduler_job_log l
GROUP BY l.job_name
) t on t.max_date = u.log_date
AND t.job_name = u.job_name

MYSQL delete all emails if email_address is in second table [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have 2 email tables. 1 = main email table, 2 = bounce_table.
Field email = unique in both tables.
Tasks = delete from main_email_table where email is in bounce_table.
I have no idea how to make the right call for action for this task.
I hope to get an idea how to solve this.
Thanks
check below query-
DELETE me.* FROM main_email AS me
JOIN bounce_email AS be ON me.email=be.email;
You need a DELETE with JOIN:
DELETE email_table
FROM email_table
INNER JOIN bounce_table ON bounce_table.email = email_table.email

Linking two MySQL tables [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Even this is asked a billion times here already, none of them has worked for me.
So: I have two tables:
banhammer_bans:
and banhammer_players:
What sort of query should I make that it gets the name value from the "players" table corresponding to the "player_id" and "creator_id" value? I've tried with JOINS and UNIONS but no success.
Select p.name, p.id as player_id, b.creator_id as creator_id
from banhammer_bans as b
inner join banhammer_players as p on p.id = b.player_id
You can use something like this:
select
*
from
banhammer_bans, banhammer_players
where
banhammer_players.player_id = banhammer_bans.id
This is a kind of Join too, but it has some sort of efficiency problems.