Very confused why this is happening since I'm naming the specific tables as es and sub that it's complaining about (and I think that's how you fix that particular issue).
I've got:
users submissions_comments email_settings submissions tables
This query gives me what I want, but I now want to join this on my email_settings and submissions table which both have a user_id.
SELECT user_id,
submission_id,
comment,
users.*,
FROM submissions_comments
INNER JOIN
(SELECT submissions_comments.parent_id
FROM submissions_comments
WHERE id = 224) x ON submissions_comments.id = x.parent_id
LEFT JOIN users ON submissions_comments.user_id = users.id
Output:
user_id: 35
submission_id: 12
comment: fdasadfsdadfs
id: 35
email: bobcobb#gmail.com
username: bobcobb
name: Robert Cobb
about:
created: 2014-03-21 20:24:57
last_login: 2014-07-06 23:21:43
public_profile: 1
queued_photo: 0
But if I try to join on another table (which has a reference to a user_id) I get Column 'user_id' in field list is ambiguous
SELECT user_id,
submission_id,
comment,
users.*,
es.*,
sub.*
FROM submissions_comments
INNER JOIN
(SELECT submissions_comments.parent_id
FROM submissions_comments
WHERE id = 224) x ON submissions_comments.id = x.parent_id
LEFT JOIN users ON submissions_comments.user_id = users.id
LEFT JOIN submissions sub ON x.parent_id = sub.user_id
LEFT JOIN email_settings es ON x.parent_id = es.user_id;
As you can tell I'm joining all of these on the user_id returned from the inner select (e.g. x.parent_id).
You have to add the alias to the column name:
SELECT sub.user_id,...
SELECT submissions_comments.user_id,
submissions_comments.submission_id,
submissions_comments.comment,
users.*,
es.*,
sub.*
FROM submissions_comments
INNER JOIN
(SELECT submissions_comments.parent_id
FROM submissions_comments
WHERE id = 224) x ON submissions_comments.id = x.parent_id
LEFT JOIN users ON submissions_comments.user_id = users.id
LEFT JOIN submissions sub ON x.parent_id = sub.user_id
LEFT JOIN email_settings es ON x.parent_id = es.user_id;
you need to define which user_id you are referring on the select part as well.
Related
i'm trying to build a chat application, in which i have these four tables
chat --id,group_id,body,time_posted[timestamp]
chat_groups --establish id,name for a group
users_groups --link users to a group, also define if user stared this group FIELDS::user_id,group_id,stared[bool]
wall_visit --user_id,group_id,last_visit[timestamp]
the idea is every users join a group, and they post to it in chat.
chat_groups table is just for defining the room, while users_groups is for setting access of members to this group.
wall_visit table is a table that store specific user last time accessed specific group (since its many to many u know..)
now what im trying to establish is to get in one query,
the chat_groups the user in relation with
the count of messages posted to this group since user last login (from settings)
the count of members in this group
the group name
:)
i have been trying to hours now :( best i could come up with
SELECT w.last_visit,access.stared,cg.user_id,u.fullname as username,cg.name as group_name ,cgu.count_members,c.count_msgs,c.time_posted
FROM `chat_groups`cg
inner join chat_groups_users access on (access.group_id = cg.id and access.user_id = ?)
left outer join users u on u.id = cg.user_id
left join wall_visit w on w.group_id = cg.id
left join (select count(*) as count_members,group_id from group_users group by group_id) cgu on cgu.group_id = cg.id
left join (SELECT count(wv.id) as count_msgs,c.group_id,c.time_posted FROM chats c
left outer join `wall_visit` wv on (wv.group_id is not null and c.group_id = wv.group_id and c.time_posted > wv.last_visit)
group by c.group_id) c on c.group_id = cg.id
where cg.user_id = 1
this query is working ..ehh, my main problem is with the count of the messages in the group since last_visit.
what is the best methode to get message_count to work :( ??
can this query be optimized more?
Thanks SO community :)
My 2nd attempt
SELECT w.last_visit,access.stared,cg.user_id,u.fullname as username,cg.with_id,uu.fullname as with_name,cg.name as group_name ,cgu.count_members,c.count_msgs,c.time_posted
FROM `chat_groups`cg
inner join chat_groups_users access on (access.group_id = cg.id and access.user_id = 1)
left outer join users u on u.id = cg.user_id
left join wall_visit w on w.chat_id = cg.id
left outer join users uu on uu.id = cg.with_id
left join (select count(*) as count_members,group_id from chat_groups_users group by group_id) cgu on cgu.group_id = cg.id
left join (
SELECT group_id,count(c.id) as count_msgs,time_posted FROM `chats` c inner join wall_Visit wv on wv.chat_id = c.group_id where c.id > wv.last_visit group by c.group_id
) c on c.group_id = cg.id
where cg.user_id = 1
this should fix you count message problem
SELECT
`cg`.`user_id`, `cg`.`with_id`, `cg`.`name` AS `group_name`,
`access`.`stared`,
`u`.`fullname` AS `username`,
`w`.`last_visit`,
`uu`.`fullname` AS `with_name`,
`cgu`.`count_members`,
`c`.`count_msgs`, `c`.`time_posted`
FROM `chat_groups` AS `cg`
INNER JOIN `chat_groups_users` AS `access`
ON (`access`.`group_id` = `cg`.`id` AND `access`.`user_id` = `cg`.`user_id`)
LEFT OUTER JOIN `users` AS `u`
ON (`u`.`id` = `cg`.`user_id`)
LEFT JOIN `wall_visit` AS `w`
ON (`w`.`chat_id` = `cg`.`id`)
LEFT OUTER JOIN `users` AS `uu`
ON (`uu`.`id` = `cg`.`with_id`)
LEFT JOIN (
SELECT COUNT(*) AS `count_members`, `group_id`
FROM `chat_groups_users`
GROUP BY
`group_id`
) AS `cgu`
ON (`cgu`.`group_id` = `cg`.`id`)
LEFT JOIN (
SELECT count(`c`.`id`) AS `count_msgs`, `c`.`time_posted`
FROM `chats` AS `c`
INNER JOIN `wall_visit` AS `wv`
ON (`wv`.`chat_id` = `c`.`group_id`)
WHERE
`c`.`time_posted` > `wv`.`last_visit`
GROUP BY
`c`.`group_id`
) AS `c`
ON (`c`.`group_id` = `cg`.`id`)
WHERE `cg`.`user_id` = 1
otherwise u have to setup a fiddle
I have 3 tables: tickets, tickets_users and users. My problem is that in the users table I have 2 types of users: requesters and solvers: type 1 and 2.
I want to select the ticket, requester (if any, can be multiple) and solver (if any, can be multiple).
I'm thinking something in the lines of:
SELECT
t.id, t.description,
(u.id where u.type = 1) AS requester,
(u.id where u.type = 2) AS solver
FROM
tickets t
INNER JOIN
tickets_users tu ON t.id = tu.ticket_id
INNER JOIN
users u ON tu.user_id = u.id
Obviously this does not work.
The tables look like this:
Tickets:
ID Description
1 Description 1
2 Description 2
Tickets_users
ID Ticket_ID User_id Type
1 3 4 1
2 5 8 2
Users
ID Name
1 John
2 Mary
Thanks,
In the meantime I think I found a solution using a sub-query in the join clause, but to me it looks rudimentary:
SELECT
t.id, t.name AS ticket_name, type1.users_id AS requester,
type2.users_id AS solver
FROM
tickets t
INNER JOIN
(SELECT users_id, tickets_id
FROM tickets_users
WHERE TYPE = 1) type1 ON t.id = type1.tickets_id
INNER JOIN
(SELECT users_id, tickets_id
FROM tickets_users
WHERE TYPE = 2) type2 ON t.id = type2.tickets_id
Should be something like this.
SELECT
t.id, t.description, u.id, userType
CASE WHEN u.type = 1 THEN 'requester' ELSE 'solver' END
FROM
tickets t
INNER JOIN tickets_users tu ON t.id = tu.ticket_id
INNER JOIN users u ON tu.user_id = u.id
Please provide sqlfiddle in case above example does not work.
With Joins,
SELECT
t.id, t.description, requesters.id, solvers.id
FROM
tickets t
INNER JOIN tickets_users tu ON t.id = tu.ticket_id
LEFT JOIN users requesters ON (tu.user_id = requesters.id AND requesters.type=1)
LEFT JOIN users solvers ON (tu.user_id = solvers.id AND solvers.type=2)
As you can tell, users table is joined twice (as requesters and as solvers with additional conditions). The reason LEFT JOIN is used is because if there's a record with no requesters or no solvers, INNER JOIN would completely ignore the whole record until it has a requester and a solver.
Here is the query in question:
SELECT e.id, e.name, u.x_account_username, ao.id
FROM x_evidence e
LEFT JOIN x_ambition_owner ao
ON e.ambition_id = ao.id
LEFT JOIN x_user u
ON ao.profile_id = u.id
WHERE e.updated BETWEEN '2014-03-19 10:16:00' AND '2014-03-19 11:16:00'
Can anyone see what I'm doing wrong?
Evidence table:
id ambition_id name updated
1jk 2ef abc 2014-03-19 10:33:31
Ambition owner table:
id ambition_id profile_id
1op 2ef 1abc
User table:
id x_account_username
1abc rex hamilton
I want my returning to get:
the evidence id, name, user's name, the associated ambition id, between the times stated (but these could vary)
Thanks
One way it is not working is that the where clause is turning the first left join into an inner join. So, you would not be getting all rows from the query that you expect. In fact, you might not be getting any of them.
You can fix this by moving the condition into the on clause:
SELECT ao.id AS a_id, e.id AS e_id, e.name AS evidence_name,
u.x_account_username AS username, 'evidence_edit' AS type
FROM x_ambition_owner ao
LEFT JOIN x_evidence e
ON ao.id = e.ambition_id
AND e.updated BETWEEN '2014-03-19 10:16:00' AND '2014-03-19 11:16:00'
LEFT JOIN x_user u
ON ao.profile_id = u.id;
EDIT (by OP):
This is the query I will be using:
SELECT e.id, e.name, u.x_account_username AS username, a.ambition_id
FROM x_evidence e
LEFT JOIN x_ambition_owner a
ON e.ambition_id = a.ambition_id
LEFT JOIN x_user u
ON a.profile_id = u.id
WHERE e.updated BETWEEN '2014-03-19 10:00:00' AND '2014-03-19 11:16:00'
I have following tables:
Table: user
Columns
- id
- username
- full_name
Table: pet
Columns
- id
- pet_name
Table: results
Columns
- id
- user_id_1
- user_id_2
- user_id_3
- pet_id
- date
- some_text
Can I make an SQL query witch will give me one row with: id, full_name, full_name, full_name, pet_name, date, some_text where the results.id is 3?
select u1.full_name
, u2.full_name
, u3.full_name
, p.pet_name
, r.date
, r.some_text
from Results r
join User u1
on u1.id = r.user_id_1
join User u2
on u2.id = r.user_id_2
join User u3
on u3.id = r.user_id_3
join pet p
on p.id = r.pet_id
where r.id = 3
Try Following query :
SELECT A.id, B.full_name, C.full_name, D.full_name, E.pet_name, A.date, A.some_text
FROM RESULTS AS A
LEFT OUTER JOIN USER AS B ON A.USER_ID1 = B.ID
LEFT OUTER JOIN USER AS C ON A.USER_ID2 = C.ID
LEFT OUTER JOIN USER AS D ON A.USER_ID3 = D.ID
LEFT OUTER JOIN PET AS E ON A.PET_ID = E.ID
WHERE A.id = 3
I have four tables
1. tbl_threads
2. tbl_comments
3. tbl_votes
4. tbl_users
suppose the currently logged in user_id =3
thread_id = 10
Now I have to retrieve the following data
All the fields from tbl_comments where tbl_comments.thread_id =10
All the fields from tbl_users based on the common key tbl_users.user_id = tbl_comments.user_id
All the fields from tbl_votes Where user_id =3 And tbl_votes.comment_id =tbl_comments.comment_id
How can I perform all this function with one single query?
I have tried the following query but it gives me the wrong results
SELECT tbl_comments.*
, tbl_users.*
, tbl_votes.*
FROM tbl_comments
INNER JOIN tbl_users
on tbl_comments.user_id = tbl_users.user_id
WHERE thread_id= 10
INNER JOIN tbl_votes
on tbl_votes.comment_id = tbl_comments.comment_id
WHERE tbl_votes.user_id= 3
Assuming the column thread_id is in the tbl_comments table, change the first where to and:
SELECT tbl_comments.*
, tbl_users.*
, tbl_votes.*
FROM tbl_comments
INNER JOIN tbl_users
on tbl_comments.user_id = tbl_users.user_id
and tbl_comments.thread_id= 10
INNER JOIN tbl_votes
on tbl_votes.comment_id = tbl_comments.comment_id
WHERE tbl_votes.user_id= 3
And although your question mentions a table named tbl_threads, you don't show any reference to it in your example.
SELECT O.OrderNumber, CONVERT(date,O.OrderDate) AS Date,
P.ProductName, I.Quantity, I.UnitPrice
FROM [Order] O
JOIN OrderItem I ON O.Id = I.OrderId
JOIN Product P ON P.Id = I.ProductId
ORDER BY O.OrderNumber