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 3 years ago.
Improve this question
I have one table like this.
id conversation_id messageable_id
1 1 3 <--
2 1 5
3 2 7
4 2 3 <--
5 3 7
6 3 9
One conversation has two member.
If sender is 3, I have to get all conversation and receiver like following.
conversation_id sender_id recevicer_id [sic]
1 3 5
2 3 7
How can I build query?
Thanks in advance.
You could use lead() (available in MySQL 8.0):
select *
from (
select
conversation_id,
messageable_id sender_id,
lead(messageable_id) over(partition by conversation_id order by id) receiver_id
from mytable
) t
where sender_id = 3
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 3 years ago.
Improve this question
i hava a table
id sentUID recUID Amount
1 1 2 100
2 2 1 100
3 4 2 100
4 2 1 100
5 8 6 100
6 2 9 100
and i want to get userID 2 record like this
id sentUID recUID Amount
1 1 2 100
3 4 2 100
6 2 9 100
only three UserID 1, 4, 9 sent or recive amount.
I think you want:
select t.*
from t
where 2 in (sentUID, recUID)
order by id;
Try this:
SELECT * FROM data WHERE id IN
(SELECT tab1.mId FROM
(SELECT MIN(id) as mId, CASE WHEN sentUID=2 THEN recUID
WHEN recUID=2 THEN sentUID
END AS person
FROM data WHERE recUID=2 OR sentUID=2
GROUP BY person)
AS tab1);
result:
id sentUID recUID Amount
1 1 2 100
3 4 2 100
6 2 9 100
the intermediate table tab1 lists each of the unique UIDs that appear with 2 and is used to calculate the final result:
mId person
1 1
3 4
6 9
http://sqlfiddle.com/#!9/16f6a5/13/0
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 4 years ago.
Improve this question
This is my sql outcome:
What I am trying to achieve is to show the garden and under that it shows the hose pipe ,lawn moar etc.
How to write the query?
If I understand you correct you would normaly do that somehow like that.
cat_id | parent_id | cat_name | cat order
1 0 Garden 1
2 1 Hose Pipe 1
3 1 Lawn Moar 2
in this case Garden for example would have the cat_id 1 and parent_id 0 and all subcategories have their own incremented id and the parent_id of garden, which would be 1 in this case!
then you can query for your categories and foreach of them query for the subcategories where the parent_id is the cat_id of the maincategory!
here you have a nice tutorial on how to display it recursive:
https://www.codexworld.com/dynamic-category-subcategory-tree-php-mysql/
that way you also only need to save the imagepath only once in your maincat!
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 5 years ago.
Improve this question
Example: I want to get all users who do not follow the id 2 user
Table Followers
id | user_id | follower_id
1 2 7
2 2 8
Table users
id | username | e-mail | group
Give this a shot:
Select users.*
from users
where users.id not in (
select followers.follower_id
from followers
where followers.user_id = "2"
)
I'm not a mysql expert but I believe a pseudo-sql would be:
select users.* from users where users.id is in (select distinct follower_id from followers where followers.user_id != 2)
The != means different.
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 8 years ago.
Improve this question
i have here
table_A
| Customer ID | moneyspent |
001 50
002 30
003 20
003 20
002 30
i have seen a query that gets the sum of all moneyspent from table A
SELECT SUM(moneyspent) FROM table_A
but i want the results to be inserted in table B's column"totalspent" like this
table_B
| Customer ID | totalspent |
001 50
002 60
003 40
help pls.
thanks
Try this, it works I've checked:
INSERT INTO table_B (Customer_ID, totalspent)
(SELECT Customer_ID, sum(moneyspent) FROM table_A group by Customer_ID)
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 8 years ago.
Improve this question
I'm looking for the right syntax using Zend db to show a table that is sorted by the count. For example, my mysql table looks like this:
Users Description
1 topic1
1 topic2
2 topic3
I want the output to look like:
User 1 (2 descriptions)
User 2 (1 description)
The raw query you're looking for
SELECT users, COUNT(*) count
FROM table1
GROUP BY users
ORDER BY COUNT(*) DESC
Output:
| USERS | COUNT |
|-------|-------|
| 1 | 2 |
| 2 | 1 |
Here is SQLFiddle demo
I'm not an expert in Zend but your query may look something between the lines of
$select = $db->select()
->from('table1', array('users', 'count' => '(COUNT(*))'))
->group('users')
->order('(COUNT(*)) DESC');
$stmt = $db->query($select);
$result = $stmt->fetchAll();