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 ('','','',)"
Related
I have a list of ids pre-generated that I need to check if exist in a table. My table has two columns, id, name, where id is an auto increment integer and name is a varchar(255).
I basically want to get a count of how many ids do not exist in table foo from my pre-generated list. So say my list has the numbers 5 and 10 in it, what's the best way to write something to the extent of:
select count(*) from foo where id does not exist in ( 5, 10 )
The idea here is that if 5 and 10 do not exist, I need the response 2, and not the number of rows in foo that do not have the id 5 or 10.
TL; DR sample data and queries at rextester
The idea here is that if 5 and 10 do not exist, I need the response 2, and not the number of rows in foo that do not have the id 5 or 10.
You should have provided a little more information to avoid confusion.
Example
id | name
1 | tom
2 | joe
3 | mae
4 | goku
5 | vegeta
If your list contains (1, 2, 3) then your answer should be 0 (since all three are in the table )
If your list contains (1, 2, 6) then your answer should be 1. ( since 1 and 2 are in the table but 6 is in't )
If your list contains (1, 6, 7) then your answer should be 2.
If your list contains (6, 7, 8) then your answer should be 3.
assuming this was your question
If you know the length of your list
select 2 - count(*) as my_count from foo where id in (5, 10)
The following query tells you how many are present in foo.
select count(*) from foo where id in (5,10)
So if you want to find those that do not exist, subtract this result from the length of your list.
select n - count(*) as my_count from foo where id in (5, 10,....)
You could use on fly table using union and the a left join
select count(*)
from my_table as m
left join (
select 5 as id from dual
union
select 10 from dual ) t on t.id = m.id
where t.id is null
otherwise you can populate a tempo table with the value you need and use left join
where the value is null
I have a table like this:
userid | trackid | path
123 70000 ad
123 NULL abc.com
123 NULL Apply
345 70001 Apply
345 70001 Apply
345 NULL Direct
345 NULL abc.com
345 NULL cdf.com
And I want a query like this. When path='abc.com', num_website +1; when path='Apply', num_apply +1
userid | num_website | num_Apply | num_website/num_Apply
123 1 1 1
345 1 2 0.5
My syntax looks like this:
select * from
(select userid,count(path) as is_CWS
from TABLE
where path='abc.com'
group by userid
having count(path)>1) a1
JOIN
(select userid,count(userid) as Apply_num from
where trackid is not NULL
group by userid) a2
on a1.userid=a2.userid
My question is
1. how to have the field num_website/num_apply in term of my syntax above?
2. is there any other easier way to get the result I want?
Any spots shared will appreciate.
The simplest way to do it would be to change the select line:
SELECT a1.userid, a1.is_CWS, a2.Apply_num, a1.is_CWS/a2.Apply_num FROM
(select userid,count(path) as is_CWS
from TABLE
where path='abc.com'
group by userid
having count(path)>1) a1
JOIN
(select userid,count(userid) as Apply_num
from TABLE
where trackid is not NULL
group by userid) a2
on a1.userid=a2.userid
and then continue with the rest of your query as you have it. The star means "select everything." If you wanted to select only a few things, you would just list those things in place of the star, and if you wanted to select some other values based on those things, you would put those in the stars as well. In this case a1.is_CWS/a2.Apply_num is an expression, and MySql knows how to evaluate it based on the values of a1.is_CWS and a2.Apply_num.
In the same vein, you can do a lot of what those subqueries are doing in a single expression instead of a subquery. objectNotFound has the right idea. Instead of doing a subquery to retrieve the number of rows with a certain attribute, you can select SUM(path="abc.com") as Apply_num and you don't have to join anymore. Making that change gives us:
SELECT a1.userid,
SUM(path="abc.com") as is_CWS,
a2.Apply_num,
is_CWS/a2.Apply_num FROM
TABLE
JOIN
(select userid,count(userid) as Apply_num
FROM TABLE
where trackid is not NULL
group by userid) a2
on a1.userid=a2.userid
GROUP BY userid
Notice I moved the GROUP BY to the end of the query. Also notice instead of referencing a1.is_CWS I now reference just is_CWS (it's no longer inside the a1 subtable so we can just reference it)
You can do the same thing to the other subquery then they can share the GROUP BY clause and you won't need the join anymore.
to get you started ... you can build on top of this :
select
userid,
SUM(CASE WHEN path='abc.com'then 1 else 0 end ) as num_website,
SUM(CASE WHEN path='Apply' and trackid is not NULL then 1 else 0 end ) as Apply_Num
from TABLE
WHERE path='abc.com' or path='Apply' -- may not need this ... play with it
group by userid
There are a lot of questions posted on stackoverflow that are almost same like my problem but I found no working solution. I have a table message and entries are:
id | Message | Status
1 | Hello | 1
2 | Hiiii | 0
4 | Works | 1
I have another table which gives ids 1,2,3 and I want to find the status of all these entries from message table. What I want is if an id doesn't exist in message table then it should return null for that id if I use IN clause to find all status. I want following result:
id | Status
1 | 1
2 | 0
3 | null
I have been using IN clause but didn't get working output. Then I got a solution on stackoverflow and tried this query
SELECT `id`,`status` FROM ( SELECT 1 AS ID UNION ALL SELECT 2 AS ID UNION ALL SELECT 3) ids LEFT OUTER JOIN message ON ids.ID = message.id
But this query is not giving the expected results. Any help would be much appreciated.
I don't see how your query would work. The column id should be ambiguous in the select (unless your database is case sensitive). Try this:
SELECT ids.ID, m.status
FROM ( SELECT 1 AS ID UNION ALL SELECT 2 AS ID UNION ALL SELECT 3
) ids LEFT OUTER JOIN
message m
ON ids.ID = m.id;
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)
I have a table with 2 columns (see below). A member can have multiple responses to a question:
RESPONSES
---------
member_id INT
response_id INT
SAMPLE DATA
member_id -- response_id
1 -- 3
1 -- 5
2 -- 1
2 -- 5
2 -- 9
3 -- 1
3 -- 5
3 -- 6
What I need to do is query the table for member that meet ALL response criteria. For example I need to select all members that have a response_id of 1 AND 5. I am using the following query:
SELECT DISTINCT member_id
FROM responses
WHERE response_id = 1 AND response_id = 5
I would expect to get back member_id's 2 and 3. However I am getting nothing returned. I used EXPLAIN and it shows there is an error in my where query. What am I doing wrong?
Also, is there a function similar to IN where all the criteria must be met in order to return true?
This should work:
SELECT member_ID
FROM responses
WHERE response_ID IN (1,5)
GROUP BY member_ID
HAVING COUNT(DISTINCT response_id) = 2
You need to count the number of records returned which is equal to the number of values supplied in your IN clause.
SQLFiddle Demo