how to subtract one query result to another query in mysql - php

this is my first query
SELECT DISTINCT(u.nickname) FROM user u
where u.id IN(SELECT `submission_user`.`user_id` from `submission_user`) AND
u.member_since >= '2015-07-01'
this is my second query
SELECT DISTINCT(u.nickname) FROM user u LEFT JOIN submission_user su ON su.user_id = u.id
LEFT JOIN submission s ON s.id = su.submission_id
WHERE s.date_time BETWEEN '2017-10-31' and '2018-07-31'
and this is my third query
SELECT DISTINCT(u.nickname) FROM user u LEFT JOIN submission_user su ON su.user_id = u.id
LEFT JOIN track_user tu ON tu.user_id = u.id
LEFT JOIN track ON track.id = tu.track_id
where track.uploaded_timestamp BETWEEN '2017-10-31' and '2018-07-31'
and after that, I am merging the second and third query result
$ids_reactivated = array_unique(array_merge($track_user, $submit_user));
so my question is that if I want to subtract query one result to merge result means with the (query 2 and 3)i.e in my case: $ids_reactivated
anyone have an idea how to do it ...
I already tried many ways and passed one day...
hope pepls help me thanks

thanks for your hint but i got my answer like this ...
SELECT DISTINCT(u.nickname) FROM user u
where u.id IN(SELECT `submission_user`.`user_id` from `submission_user`) AND
u.member_since >= '2015-07-01'
**and u.nickname not in ($query2)**
UNION
SELECT DISTINCT(u.nickname) FROM user u
where u.id IN(SELECT `submission_user`.`user_id` from `submission_user`) AND
u.member_since >= '2015-07-01'
**and u.nickname not in ($query3)**

Its simple You can make use of union for merging and not in for substraction
Following is the sample
SELECT DISTINCT(u.nickname) FROM user u LEFT JOIN submission_user su ON su.user_id = u.id LEFT JOIN submission s ON s.id = su.submission_id
WHERE s.date_time BETWEEN '2017-10-31' and '2018-07-31'
**and u.nickname not in ($query1)**
UNION
SELECT DISTINCT(u.nickname) FROM user u LEFT JOIN submission_user su ON su.user_id = u.id LEFT JOIN track_user tu ON tu.user_id = u.id LEFT JOIN track ON track.id = tu.track_id
where track.uploaded_timestamp BETWEEN '2017-10-31' and '2018-07-31'
**and u.nickname not in ($query1)**
change the $query1 with your query ,it should give the result

Related

How to perform inner join in mysql

I have to write a query such that ,I need to get events whose start date is of 30 min from now.
My conditions are:
1) get the event from events table
2)Join created by of events with id in users table.
3)Comments from comment table with user ser id
But the problem here is if there is no comment for event then the event it self is not coming.If any comment is present it is coming.I dont want this.If comment is not there just fetch it as empty but not hide the total event .Can anyone please help me,.Thanks.
select u.email ,group_members.user_id,users.first_name,u.first_name
as host_name,events.name,events.start_date,comments.comments,c.first_name as
comment_user,comments.id from events
inner join users as u on u.id = events.created_by
inner join comments on events.id = comments.event_id
inner join group_members on events.group_id = group_members.group_id
inner join users as c on comments.from_user = c.id
inner join users on group_members.user_id = users.id
where events.start_date between date_add(now(),interval 1 minute) and date_add(
now(),interval 30 minute)
and group_members.user_status = 2
and events.status = 2
You need a left join to the comments table. I would put that table last in the from clause.
select u.email, gm.user_id, gu.first_name, u.first_name as host_name,
e.name, e.start_date, c.comments, uc.first_name as comment_user,
c.id
from events e inner join
users u
on u.id = e.created_by inner join
group_members gm
on e.events.group_id = gm.group_id inner join
users gu
on gm.user_id = gu.id left join
comments c
on e.id = c.event_id left join
users uc
on c.from_user = uc.id
where e.start_date between date_add(now(),interval 1 minute) and date_add(now(),interval 30 minute) and
gm.user_status = 2 and
e.status = 2;
Once you use a left join on comments, you also need a left join for the from user. I replaced all table names with aliases -- this makes it easier to track which table is used for which purpose.
Use the INNER JOIN Keyword and select the two columns by putting them with keyword ON.
SELECT EMP.EMP_ID, EMP.EMP_NAME, DEPT.DEPT_NAME FROM EMP
INNER JOIN DEPT ON DEPT.DEPT_ID = EMP.DEPT_ID;

GROUP_CONCAT too slow

Hello I added GROUP_CONCAT function to my query and that function killed my query :/.
My query is :
SELECT u.username,a.user_id,a.id,a.text,a.lang as fromLang,b.lang as toLang,GROUP_CONCAT(DISTINCT b.id) AS translation_ids FROM sentence as a
INNER JOIN sentence_relationship as sr ON
(sr.sentence_id = a.id)
INNER JOIN sentence as b ON
(b.id = sr.translation_id AND a.id = sr.sentence_id)
INNER JOIN users as u ON
(u.id = a.user_id) GROUP BY a.id LIMIT 10;
What is wrong with that query ?
This is your query (somewhat formatted):
SELECT u.username, a.user_id, a.id,a.text,a.lang as fromLang, b.lang as toLang,
GROUP_CONCAT(DISTINCT b.id) AS translation_ids
FROM sentence a INNER JOIN
sentence_relationship sr
ON sr.sentence_id = a.id INNER JOIN
sentence b
ON b.id = sr.translation_id AND a.id = sr.sentence_id INNER JOIN
users as u
ON u.id = a.user_id
GROUP BY a.id
LIMIT 10;
It is unclear from your question whether the group_concat() was added with the group by. That could slow things down.
The limit 10 is taking the first 10 a.ids that match (the group by does an implicit ordering). If you do this with a subquery, it will probably speed up the query:
SELECT u.username, a.user_id, a.id,a.text,a.lang as fromLang, b.lang as toLang,
GROUP_CONCAT(DISTINCT b.id) AS translation_ids
FROM (select s.*
from sentence s
order by a.id
limit 10
) a INNER JOIN
sentence_relationship sr
ON sr.sentence_id = a.id INNER JOIN
sentence b
ON b.id = sr.translation_id AND a.id = sr.sentence_id INNER JOIN
users as u
ON u.id = a.user_id
GROUP BY a.id;
This assumes that all the joins do work and match records. If the joins are used for filtering, then you may get fewer than 10 rows back.

MySQL: Select rows where `project_id` equals any `project` with specific `user_id`

This can't be too hard, but I don't know what the term is I'm looking for. I'm sure you guys can help me out. :)
I have a table tasks with rows that have a column project_id. Each project_id refers to (the id of) a row in the projects table. Each project belongs to a certain user which is why it has a column user_id.
I now want to select all tasks from this table where the project_id equals any project of a certain user.
Or put more simply:
Each TASK belongs to a PROJECT which belongs to a USER.
I want to create a SELECT-statement to receive all TASKS that belong to a specific USER. The only link between each TASK and a USER is through the PROJECT.
How do I accomplish this?
Thank you very much for your help! :)
JOIN the tables:
SELECT
t.*
FROM tasks t
INNER JOIN projects p ON t.project_id = p.project_id
INNER JOIN users u ON p.user_id = u.user_id
WHERE u.user_id = #AcertianUserId
SELECT
tasks.*
FROM
users
INNER JOIN
projects ON users.id = projects.user_id
INNER JOIN
tasks ON projects.id = tasks.project_id
WHERE
users.id = 1
SQL Fiddle
select t.*
from tasks t
join projects p on p.id = t.project_id
join users u on u.id = p.user_id
where u.id = 17
Try :
SELECT t.*
FROM user u, projet p, task t
WHERE u.id = p.user_id
AND p.id = t.project_id
AND u.id = your_id
Or (same result) :
SELECT t.*
FROM user u
INNER JOIN projet p ON u.id = p.user_id
INNER JOIN task t ON p.id = t.project_id
WHERE u.id = your_id

mysql left joining same table twice

I'm trying to left join the same table twice but it's giving me some problems. I have two tables ee_all and ee_calendar_events that I want to join.
SELECT
u.first_name,
u.last_name,
u.email,
SUM(e1.total_vacation_hours_earned) AS vacation_hours_earned,
SUM(e2.absent_hours)
FROM ee_all AS u
LEFT JOIN ee_calendar_events AS e1 ON u.user_id = e1.sched_user_id
LEFT JOIN ee_calendar_events AS e2 ON u.user_id = e2.sched_user_id AND e2.event_id = 2
WHERE
u.user_id = 23
The vacation_hours_earned column is supposed to return 133, which it does if I take out the second join. But soon as I add it, the query takes forever and the vacation_hours_earned has a value of 2000 or something (which is wrong). My guess is it's summing the row again when I add the second join, but I don't wan't that. I've been trying for a few hours but can't find a way around it, would appreciate any help.
When the rightmost table (the second join) has more than one row corresponding to a row of the table expression to the left, rows of the left-hand table expression are duplicated and count more than once in the SUM. Use subqueries instead.
SELECT
u.first_name,
u.last_name,
u.email,
(
SELECT
SUM(e1.total_vacation_hours_earned)
FROM
ee_calendar_events AS e1
WHERE
u.user_id = e1.sched_user_id
) AS vacation_hours_earned,
(similar) AS absent_hours
FROM
ee_all AS u
WHERE
u.user_id = 23
Using some MySQL syntax, you can just eliminate the second left join and simplify the query;
SELECT
u.first_name,
u.last_name,
u.email,
SUM(e1.total_vacation_hours_earned) AS vacation_hours_earned,
SUM(e1.absent_hours * (event_id=2))
FROM ee_all AS u
LEFT JOIN ee_calendar_events AS e1 ON u.user_id = e1.sched_user_id
WHERE
u.user_id = 23
Demo here.
SELECT
u.first_name,
u.last_name,
u.email,
SUM(e1.total_vacation_hours_earned) AS vacation_hours_earned,
(select SUM(e2.absent_hours) as absenthours from ee_calendar_events AS e2 where u.user_id = e2.sched_user_id AND e2.event_id = 2)
FROM ee_all AS u
LEFT JOIN ee_calendar_events AS e1 ON u.user_id = e1.sched_user_id
WHERE
u.user_id = 23

Newest loan per user for all users

// Edit:
All work fine with:
SELECT u.*,
l.cod AS loans_cod,
l.step AS loans_step
FROM users AS u
LEFT JOIN loans AS l
ON u.id = l.users_id
WHERE l.step < 12
OR NOT EXISTS
(SELECT l.id
FROM loans
WHERE l.users_id = u.id
)
GROUP BY u.id
Now, I can select all user, and his last loan. Thanks, and I think my solution will help future users.
if you want the last loan, you could append something like this
ORDER BY l.date DESC GROUP BY u.id
SELECT u.*,
l.cod AS loans_cod,
l.step AS loans_step
FROM users AS u
LEFT JOIN loans AS l
ON u.id = l.users_id
WHERE u.id != :id
AND l.timestamp = (SELECT MAX(l.timestamp)
FROM loans AS l2
WHERE l2.users_id = l.users_id)
Where timestamp is a column on your table that indicates when the loan was created or otherwise gives you a time that you can compare to.
Try this:
SELECT u.*, l.cod AS loans_cod,
l.step AS loans_step
FROM users AS u inner join
(
select max(l.loan_id) as loan_id, l.users_id , l.cod, l.step
from loans l
group by
l.users_id,l.cod,l.step
) as l
ON u.id = l.users_id
WHERE u.id != :id
Note: Avoid doing select * it's a very bad practice. List all the columns you need only.

Categories