I have following query .. in Dev_phones I have 90 000 item in Dev_users 50 000
select `u`.`id` from `Dev_users` as `u`
left join `Dev_user_usergroup_map` as `m` on `u`.`id` = `m`.`user_id`
left join `Dev_phones` as `p` on `m`.`id` = `p`.`acc_id` and `primary`=1
where `u`.`email`='sample#gmail.com' or `u`.`private_number`='sample#gmail.com'
or `m`.`id`='sample#gmail.com' or `p`.`number`='sample#gmail.com' limit 1
query goes to loop and not ending ...
what is problem and how can i write better this query
Just FYI, this query can be rewritten as follows - although I find using OUTER JOIN with OR (or an IN) across several tables in this way very confusing...
SELECT u.id
FROM Dev_users u
LEFT
JOIN Dev_user_usergroup_map m
ON u.id = m.user_id
LEFT
JOIN Dev_phones p
ON m.id = p.acc_id
AND `primary` = 1
WHERE 'sample#gmail.com' IN(u.email,u.private_number,m.id,p.number)
LIMIT 1;
Related
I have a huge select query where i have to join more than 85 tables. I keep getting an error when running the query, if I re-run the query when shrinking the overall statement it runs fine.
See a portion of the join below, it does that all the way to table 85:
select $imploded_tables from $apps a
left join $mobile_c0 $m_c0 on $apps.id = $m_c0.id
left join $mobile_c1 $m_c1 on $m_c0.id = $m_c1.id
left join $mobile_c2 $m_c2 on $m_c1.id = $m_c2.id
left join $mobile_c3 $m_c3 on $m_c2.id = $m_c3.id
left join $mobile_c4 $m_c4 on $m_c3.id = $m_c4.id
left join $mobile_c5 $m_c5 on $m_c4.id = $m_c5.id
left join $mobile_c6 $m_c6 on $m_c5.id = $m_c6.id
left join $mobile_c7 $m_c7 on $m_c6.id = $m_c7.id
left join $mobile_c8 $m_c8 on $m_c7.id = $m_c8.id
left join $mobile_c9 $m_c9 on $m_c8.id = $m_c9.id
left join $mobile_c10 $m_c10 on $m_c9.id = $m_c10.id
...
...
Mysql Documentation
The maximum number of tables that can be referenced in a single join is 61.
As per #sf_admin answer the maximum no of join is 61 , so you can do something like below
select imploded_tables from apps a
JOIN
(
SELECT * from mobile_c0
UNION
SELECT * from mobile_c1
....
)tmp
where(tmp.id=a.id)
It might not 100% percent answer your question but this is some work around I did
Hello i'm learning sql and i have some issues with joins(which i have problems understanding them)
I have this issue
#1066 - Not unique table/alias: 'tbl_respuestas'
what the query supposed to do, is count how many people(general,ignore user) has answer 'x', in 'y' question of 'z' survey
SELECT COUNT(*) FROM tbl_respuestas
INNER JOIN tbl_encuesta_usuario ON tbl_encuesta_usuario.user_id = user.id
INNER JOIN tbl_encuesta ON tbl_encuesta.id = tbl_encuesta_usuario.tbl_encuesta_id
INNER JOIN tbl_encuesta_has_tbl_preguntas ON tbl_encuesta_has_tbl_preguntas.tbl_encuesta_id = tbl_encuesta.id
INNER JOIN tbl_preguntas ON tbl_preguntas.id = tbl_encuesta_has_tbl_preguntas.tbl_preguntas_id
INNER JOIN tbl_preguntas_has_tbl_respuestas ON tbl_preguntas_has_tbl_respuestas.tbl_preguntas_id = tbl_preguntas.id
INNER JOIN tbl_respuestas ON tbl_respuestas.id = tbl_preguntas_has_tbl_respuestas.tbl_respuestas_id
WHERE tbl_respuestas.respuesta = 2
line SELECT COUNT(*) FROM tbl_respuestas
and line INNER JOIN tbl_respuestas
does not makes sense, hence the error.
Unless it is what you want then you need to give then different name/alias like below:
SELECT COUNT(*) FROM tbl_respuestas r
INNER JOIN tbl_respuestas r2
Also as a quick note you can rewrite the entire sql like below.
It is good practice to give your tables a name for shorter referencing and makes the sql look a little cleaner.
Also if both tables you are trying to join has the same column name then you can use the keyword USING instead of having to write that long line tbl_encuesta_usuario.user_id = user.id
Please be sure to put r and r2 in its prope place
SELECT COUNT(*) FROM tbl_respuestas r
INNER JOIN tbl_encuesta_usuario u USING user_id
INNER JOIN tbl_encuesta e ON e.id = u.tbl_encuesta_id
INNER JOIN tbl_encuesta_has_tbl_preguntas hp ON hp.tbl_encuesta_id = e.id
INNER JOIN tbl_preguntas p ON p.id = hp.tbl_preguntas_id
INNER JOIN tbl_preguntas_has_tbl_respuestas hr ON hr.tbl_preguntas_id = p.id
INNER JOIN tbl_respuestas r2 ON r2.id = hr.tbl_respuestas_id
WHERE r.respuesta = 2
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;
I am trying to get the left outer join of 2 tables with respect to two tables , but i am unable to execute this query , phpmyadmin is giving #1064 error on line 12 when running this query:
SELECT
pt.id as planid,
pt.trip_name,
pt.description,
cor.latitude,
cor.longitude,
bb.id as bookmarkid,
bb.num_of_persons as persons
FROM
planned_trips as pt,
coordinates as cor,
LEFT JOIN Bookmarkedby as bb,Users as user
ON
user.id = 1 AND
user.id = bb.user_id AND
bb.plannedtrips_id = pt.id AND
pt.coordinates_id = cor.id'
I've struggling for an hour , what am i missing??
my database schema looks like this:
I am Currently just preparing my query i need to run this query on codeIgnitor.
You need a separate LEFT JOIN for each table.
SELECT
pt.id as planid,
pt.trip_name,
pt.description,
cor.latitude,
cor.longitude,
bb.id as bookmarkid,
bb.num_of_persons as persons
FROM planned_trips as pt
INNER JOIN coordinates as cor ON pt.coordinates_id = cor.id
LEFT JOIN Bookmarkedby as bb ON bb.plannedtrips_id = pt.id
LEFT JOIN Users as user ON user.id = bb.user_id AND user.id = 1
Problem is in the line LEFT JOIN Bookmarkedby as bb,Users as user. Consider changing it to
FROM
planned_trips as pt
JOIN coordinates as cor ON pt.coordinates_id = cor.id
LEFT JOIN Bookmarkedby as bb ON bb.plannedtrips_id = pt.id
LEFT JOIN Users as user ON user.id = bb.user_id
AND user.id = 1
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