Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a table "Message_group" like that and AAAA is my profile_id of login.
message_group_id | profile_id | group_id
1 AAAAA 1000
2 EEEEE 1000
3 AAAAA 2000
4 FFFFF 2000
5 AAAAA 3000
6 HHHHH 3000
7 WWWWW 4000
8 RRRRR 4000
I would like a query that gives the result:
1 EEEEE 1000
2 FFFFF 2000
3 HHHHH 3000
You can join the table with itself to get the rows with the same group_id as the one you are searching for.
SELECT b.* FROM Message_group a
JOIN Message_group b ON a.group_id=b.group_id
WHERE a.profile_id = 'AAAAA'
AND b.profile_id != a.profile_id;
DEMO: http://sqlfiddle.com/#!2/7a548/1
I believe it is not a homework... So, try the following statement:
SELECT x.message_group_id, y.profile_id, y.group_id
FROM Message_group x
INNER JOIN Message_group y
ON (x.message_group_id * 2 = y.message_group_id);
If it works I'll explain you better. ^^
SELECT
yourtable.*
FROM
yourtable
WHERE
group_id IN (SELECT group_id FROM yourtable WHERE profile_id='AAAAA')
AND profile_id != 'AAAAA'
if I understand u correctly, this should do the trick:
SELECT login_id
FROM message_group
WHERE login_id <> 'AAAAA' AND group_id IN ( SELECT group_id
FROM message_group
WHERE login_id='AAAAA')
u can try it in this fiddle.
Use this:
SET #rowno = 0;
SELECT #rowno:=#rowno+1 `rn`, profile_id, group_id
FROM Message_group
WHERE profile_id = 'EEEEE' AND group_id = '1000'
|| profile_id = 'FFFFF' AND group_id = '2000'
|| profile_id = 'HHHHH' AND group_id = '3000';
and the fiddle: http://sqlfiddle.com/#!2/edf7d/10
Related
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 am beginner in sql. Anyone please tell me the problem of my customised query.
select *
from Follow_My_Doct.tbl_hospital_registration h
INNER JOIN Follow_My_Doct.tbl_subscribed_packages s
ON s.hospitalId = h.id
AND s.active = 1
AND ( select max(sp.expireDate), sp.hospitalId
from Follow_My_Doct.tbl_subscribed_packages sp
where sp.active = 1
group by sp.hospitalId )
where h.hospital_active = 1
Error Code: 1241. Operand should contain 1 column(s)
Subscription table
hospital Id expireDate
145 2021-07-10
146 2021-06-10
147 2021-09-10
146 2021-10-10
You should put that subquery with the max and group by inside an INNER JOIN clause.
select *
from Follow_My_Doct.tbl_hospital_registration h
INNER JOIN ( select max(sp.expireDate) maxexpiredate, sp.hospitalId
from Follow_My_Doct.tbl_subscribed_packages sp
where sp.active = 1
group by sp.hospitalId ) as s
ON s.hospitalId = h.id
where h.hospital_active = 1
Since I don't have your data tables, I made up one environment to test that query by using table variables. The example below is for SQL Server, but the query works fine for MySQL, which currently I don't have installed on my machine.
declare #tbl_subscribed_packages TABLE(
hospitalId int,
active bit,
expiredate datetime
)
declare #tbl_hospital_registration table(
id int,
hospital_active bit)
Now populate tables with data:
insert #tbl_hospital_registration
values (145,1),(146,1),(147,1)
insert #tbl_subscribed_packages
values (145,1,'2021-07-10')
,(146,1,'2021-06-10')
,(147,1,'2021-09-10')
,(146,1,'2021-10-10')
Then, I test the query against these data
select *
from #tbl_hospital_registration h
INNER JOIN ( select max(sp.expireDate) maxexpiredate, sp.hospitalId
from #tbl_subscribed_packages sp
where sp.active = 1
group by sp.hospitalId ) as s
ON s.hospitalId = h.id
where h.hospital_active = 1
Note that using subquery as a view in INNER JOIN, I should add an alias name for max(expireDate) column.
The result is:
id
hospital_active
maxexpiredate
hospitalId
145
1
2021-07-10 00:00:00.000
145
146
1
2021-10-10 00:00:00.000
146
147
1
2021-09-10 00:00:00.000
147
Is that what you want?
6th line has issues. After AND you should have condition but you have sub query returns two column values
You can try the below - using row_number()
select * from
(
select *,row_number() over(partition by s.hospitalId order by expireDate desc ) as rn from
Follow_My_Doct.tbl_hospital_registration h INNER JOIN Follow_My_Doct.tbl_subscribed_packages s
ON s.hospitalId = h.id
where h.hospital_active = 1 and s.active = 1
)A where rn=1
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 a table as shown below
id | t_id | u_id
---+------+------
1 | 2 | 2
2 | 1 | 2
3 | 1 | 1
4 | 2 | 2
5 | 1 | 1
I am trying to get all t_id with u_id of 2 but once without the t_id ever having a u_id of 1 in the history of the whole table.
I tried
SELECT
C_Name, count(*) as count
FROM tenter
WHERE C_Date = '20200127' AND L_TID = '2';
But this gives me the record of all L_TID = 2 and does not filter out those with previous record of L_tid = 1.
Expected result: get all U_ID without the previous history of L_TID = 1, it should get only those without ever having L_tid =1.
Thanks in advance.
One method is aggregation and a having clause:
select t_id
from tenter t
group by t_id
having sum(u_id = 2) > 0 and -- has "2"
sum(u_id = 1) = 0; -- does not have "1"
If you have another table of t values, then exists/not exists might be more efficient:
select t.t_id
from t
where exists (select 1 from tenter tt where tt.t_id = t.t_id and tt.u_id = 2) and
not exists (select 1 from tenter tt where tt.t_id = t.t_id and tt.u_id = 1);
I think you want not exists:
select t.*
from tenter t
where
u_id = 2
and not exists (
select 1 from tenter t1 where t1.t_id = t.t_id and t1.u_id = 1
)
You can also use aggregation, if you just want a list of t_ids. If 1 and 2 are the only possible values, you can just do:
select t_id
from tenter t
group by t_id
having min(u_id) = 2
If there are other possible values:
having max(u_id = 1) = 0 and max(u_id = 2) = 1
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a table:
Visit (FromId, ToId, VisitTime)
where FromId and ToId are FKs to table
UserProfile (uid, name, age ...)
As a user with my UID I want to select all profiles I have visited or who visited me in one result set ordered by VisitTime and with the indication of the "direction of the visit".
I get data using this select:
SELECT CASE WHEN a.FromID = 'yourIDHere'
THEN c.Name
ELSE b.Name
END Name,
CASE WHEN a.FromID = 'yourIDHere'
THEN c.Age
ELSE b.Age
END Age,
a.VisitTime,
CASE WHEN a.FromID = 'yourIDHere'
THEN 'You'
ELSE 'Friend'
END DirectionOfVisit
FROM Visit a
INNER JOIN UserProfile b
ON a.FromID = b.Uid
INNER JOIN UserProfile c
ON a.ToID = c.Uid
WHERE 'yourIDHere' IN (a.FromID, a.ToID)
ORDER BY a.VisitTime
Now it prints (pseudo output)
Jack (id1) | IN |12.12.2012
Jack (id1) | IN |11.12.2012
Jack (id1) | IN |11.12.2012
Jack (id1) | OUT | 13.12.2012
Jack (id1) | OUT | 12.12.2012
Michael (id5) | IN | 5.12.2012
Michael (id5) | OUT | 6.12.2012
Michael (id5) | OUT | 5.12.2012
I would like the list to be like this:
Jack | IN | 12.12.2012 (the most recent)
Jack | OUT | 13.12.2012 (the most recent)
Michael (id5) | IN | 5.12.2012 (the most recent)
Michael (id5) | OUT | 6.12.2012 (the most recent)
I know the GROUP command would solve it but it's too complex for me (beginner).
You could use GROUP BY along with an aggregate function to get the result. Since you want the most recent date for each name and type (IN/OUT), then you can use the max() aggregate function on the date column. You will then use a GROUP BY on the other columns you want to return:
The basic syntax will be:
select
name,
type,
max(date) date
from yourtable
group by name, type;
See SQL Fiddle with Demo
If you want to return the max date with your existing query, you can just expand the query to use:
select name, age, max(VisitTime), DirectionOfVisit
from
(
SELECT CASE WHEN a.FromID = 'yourIDHere'
THEN c.Name
ELSE b.Name
END Name,
CASE WHEN a.FromID = 'yourIDHere'
THEN c.Age
ELSE b.Age
END Age,
a.VisitTime,
CASE WHEN a.FromID = 'yourIDHere'
THEN 'You'
ELSE 'Friend'
END DirectionOfVisit
FROM Visit a
INNER JOIN UserProfile b
ON a.FromID = b.Uid
INNER JOIN UserProfile c
ON a.ToID = c.Uid
WHERE 'yourIDHere' IN (a.FromID, a.ToID)
) d
group by name, age, DirectionOfVisit;
See SQL Fiddle with Demo
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm making a system of chat similar to facebook, where you can talk with 2 or 3 people in the same time. My issue is on the select the conversation.
inbox_conversation (this table is for create a new id of conversation if the user start to talk the first time)
id_conversation | occured_at
1 13482942
2 18583953
inbox_join (table for user join on the conversation in this case there is user 1, 2, 3 on the disccussion with id 1)
id_conversation | id_user
1 1
1 2
1 3
2 4
2 5
inbox_msg (table for record the message sent)
id_conversation | id_user | message | occured_at
1 1 Hey 1457694
1 2 Hola 3848374
1 3 Cool 3294933
2 4 Wow 4392934
2 5 Yes 9485737
Now i have to do some query for select the messages having just the id_user of the conversation, in this case i have 1,2,3. Someone can help me to build this query please.
Final result that i'm looking for selecting the discussion with id_user 1,2,3
id_user | message | occured_at
1 Hey 1457694
2 Hola 3848374
3 Cool 3294933
PS: if the conversation have 3 users and i try to select just 2 of them i have be able to don't see the conversation.
if I have just the id of the users 1,2 in this case there is no discussion with just that 2 user, but if the user 1 write to user 2 in the database will be another id discussion and will correlate to both user. Like that if i add another user example user 3 he can't see the previous messages sent between user 1 and 2
if is a bad idea select just with id user, i can study some solution for pass the id of discussion.
ok, i did came up with logic for something called "Dataset Comparison" where multiple datasets of multiple rows were getting compared and need to find if any of them are matching.
basic point in this query will be, durig the search we need to make sure that the session we find out has exactly same user by value and by count. (nothing more+nothing less+same value)
DECLARE #inbox_msg TABLE
(
id_conversation INT NOT NULL
,id_user INT NOT NULL
,[message] NVARCHAR(MAX) NULL
,occured_at TIMESTAMP NOT NULL
)
INSERT INTO #inbox_msg
( id_conversation ,
id_user ,
message
)
SELECT 1,1,'Hey'
UNION ALL SELECT 1,2,'Hola'
UNION ALL SELECT 1,3,'Cool'
UNION ALL SELECT 2,4,'Wow'
UNION ALL SELECT 2,5,'Yes'
--this I added to make sure 3 will not become part of your result set.
UNION ALL SELECT 3,1,'Testing'
UNION ALL SELECT 3,2,'Search'
UNION ALL SELECT 3,3,'query'
UNION ALL SELECT 3,4,'Result'
--this is the list of users you want to search for
DECLARE #searchMessgeByUser TABLE
(
id_user INT NOT NULL PRIMARY KEY
)
INSERT INTO #searchMessgeByUser ( id_user )
SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3
--find out the sessionID who has exactly same number of user as participant as requested
DECLARE #MatchedSessionID TABLE
(
id_conversation INT NOT NULL
)
INSERT INTO #MatchedSessionID( id_conversation )
SELECT qry.id_conversation
FROM
(
--find out the MatchedSessionIDByUserCount
SELECT id_conversation
FROM #inbox_msg
GROUP BY id_conversation
HAVING (COUNT(DISTINCT id_user) = (SELECT COUNT(1) FROM #searchMessgeByUser) )
INTERSECT
--find out the MatchedSessionIDByUserValue
SELECT id_conversation
FROM #inbox_msg msg
JOIN #searchMessgeByUser usr
ON msg.id_user=usr.id_user
GROUP BY msg.id_conversation
)qry
--final Query
SELECT id_user,message,occured_at
FROM #inbox_msg
WHERE id_conversation IN (SELECT id_conversation FROM #MatchedSessionID)
ORDER BY occured_at
the questions are bit unclear so just making clear what exactly you need?
In the start you mentiuoned that result is queried based on ID_User only.
But in the example last you have is saying "...the result is for DiscussionID 1"
so whihc one is correct? Does DiscussionID and ID-User both available to filter or not?
Also, "PS: if the conversation have 3 users and i try to select just 2 of them i have be able to don't see the conversation"
is that mean id_User filter is of type "WHER ID_USER IN ('','','',)"
I have a MySQL table has these fields
ID, CID, QUESTION, ANSWER, USER
ID is auto increment, every record in table has ID;
CID is point to ID for ANSWER records
For example, we have 4 records, 2 of question 2 of answer, and Mike answers 2 questions
ID CID QUESTION ANSWER USER
1 0 Test NULL John
2 1 NULL This is Test Mike
3 0 Example NULL Tracy
4 3 NULL Yes it is Mike
I want to list questions of which is Mike's answers. How can I match ID and CID fields in same table and print QUESTION for output
I want to list questions of which is Mike's answers.
SELECT t1.*
FROM TableName t1
LEFT JOIN TableName t2 ON t1.ID = t2.CID
WHERE t2.Answer IS NOT NULL
AND t2.User = 'Mike';
SQL fiddle Demo
Note that this gives you the list of questions that Mike has answered therefore you won't find mike listed on them:
ID CID QUESTION ANSWER USER
1 0 Test NULL John
3 0 Example NULL Tracy
select QUESTION from yourtable
where ID in(select ID from yourtable where User = 'Mike' and answer is NOT NULL)