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 7 years ago.
Improve this question
I have a table tmp.
Query:
select * from tmp
I want the result in following way:
customer_id | subscriber_id | totalSubscribers
320 | 433 | 3
320 | 434 | 3
Can you tell me how to achieve this?
Here is solution for you question
SELECT customer_id , subscriber_id , count(*) AS totalSubscribers
FROM `tmp` GROUP BY 1,2
or
SELECT customer_id , subscriber_id , count(*) AS totalSubscribers
FROM `tmp` GROUP BY customer_id , subscriber_id
Here is screnshot for executed query
Here is the answer
SELECT customer_id , subscriber_id , count(*) as [TOTAL]
FROM tmp
GROUP BY customer_id , subscriber_id
Related
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 trying to find answer by my own, but i cant do it.
I have table:
Id | val
1 | 4
2 | 5
3 | 4
4 | 6
5 | 4
I want to select last 2 IDs with value 4.
Output should be
Id | val
3 | 4
5 | 4
The #Fahmi's query might be improved a bit:
SELECT id, val
FROM (
SELECT id, val
FROM your_tablename
WHERE val = 4
ORDER BY `id` DESC
LIMIT 2
) AS t
ORDER BY t.id ASC
Demo - http://sqlfiddle.com/#!9/eb1227/1
You can try the below -
select id, val from tablename
where val=4
order by id desc limit 2
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 5 years ago.
Improve this question
Have two tables one is customer and other prize amount
**Customer** **PrizeAmount**
*CustomerId Amount* *CustomerId PrizeAmt*
1 500 1 2000
1 2000
Resultant query should be like this
CustomerId Amount PrizeAmt
1 500 2000
1 (NULL) 2000
So how to write query for above result
With left join you can do it
SELECT column_name(s)
FROM table1
LEFT JOIN table2 ON table1.column_name = table2.column_name;
so
select customer_id as custo_id,amount,prize_amt
from customer
left join prize_amount on customer.customer_id=prize_amount.customer_id
Left Join
Perhaps this
DROP TABLE IF EXISTS T,T1;
CREATE TABLE T (CID INT , AMOUNT_INVESTED INT);
CREATE TABLE T1 (CID INT, AMOUNT_WON INT);
INSERT INTO T VALUES (1,500);
INSERT INTO T1 VALUES (1,2000),(1,2000);
SELECT CID,AMOUNT_INVESTED,AMOUNT_WON
FROM
(
SELECT T.CID,IF(T.CID <> #P, T.AMOUNT_INVESTED,NULL) AMOUNT_INVESTED, T1.AMOUNT_WON,
#P:=T.CID
FROM T
JOIN T1 ON T.CID = T1.CID
CROSS JOIN (SELECT #P:=0) P
) S
+------+-----------------+------------+
| CID | AMOUNT_INVESTED | AMOUNT_WON |
+------+-----------------+------------+
| 1 | 500 | 2000 |
| 1 | NULL | 2000 |
+------+-----------------+------------+
2 rows in set (0.00 sec)
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 7 years ago.
Improve this question
I have a problem with MySQL. I have 2 tables:
TABLE A
id | txt
1 | abc
2 | bcd
3 | cde
TABLE B
id | accept
1 | 1
2 | 0
3 | 1
I want to display txt (from table A) only if related record (the same id) accept=1 (from table B).
I can do this by setting 2 MySQL queries, but I would like to make it by one MySQL query.
Based on your comment:
SELECT txt
FROM A
LEFT JOIN B
ON A.id=B.id
WHERE B.accept = 1
SELECT A.txt
FROM A INNER JOIN B ON A.id = B.id
WHERE B.accept= 1
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 ('','','',)"
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