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.
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 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
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 7 years ago.
Improve this question
How can i make this works?
I have 2 tables:
course:
course id,name
prereq:
course id,prereq id
What is the query for this result:
+----------+------------+----------+------------+
|Course ID |Name |Prereq ID |Prereq Name |
|1 |Intro into B| | |
|2 |Biology |1 |Intro into B|
|3 |Genetics |1 |Intro into B|
+----------+------------+----------+------------+
SELECT c.courseId, c.courseName, p.prereqId, pc.courseName
FROM course as c
// joins prerequisites
LEFT JOIN prereq as p on p.courseId=c.courseId
// joins course data to prerequisites
LEFT JOIN course as pc on p.prereqId=pc.courseId
You should be able to use a left join to pull these two tables together, something like:
Select c.courseid, c.coursename, p.prere1id, p.preqname
from course as c
left join prereq as p
on c.courseid = p.courseid
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 need to get a customer list with orders quantity. Actually use query below to get customers with orders like:
+-------+----------------+
| JAMES | 2 orders total |
| PAUL | 6 orders total |
+-------+----------------+
To do this I use this query:
SELECT *
FROM mod_users
INNER JOIN (SELECT order_user_id, count(*) as order_qty
FROM mod_orders
GROUP BY user_order_id) AS order_qty
ON mod_users.user_id = order_qty.order_user_id;
Now, I'd like to get users without orders too:
+-------+----------------+
| JAMES | 2 orders total |
| PAUL | 6 orders total |
| FRANK | 0 orders total |
+-------+----------------+
Can anyone help make query to get this?
Use LEFT JOIN instead of INNER JOIN:
SELECT mod_users.user_id, COALESCE(order_qty, 0) AS ordersCount
FROM mod_users
LEFT JOIN (SELECT order_user_id, count(*) as order_qty
FROM mod_orders
GROUP BY user_order_id) AS order_qty
ON mod_users.user_id = order_qty.order_user_id;
If mod_orders doesn't contain any records for a particular user, then order_qty will be NULL due to LEFT JOIN for this user. COALESCE converts this NULL value into 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 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();
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 9 years ago.
Improve this question
I am really not good at math, but can someone help me though how i can get the place the user has on all entries? Etc. "You are now number 43 out of 20.403.044 entries"?
SELECT ?myPlace? FROM entries WHERE userid = 1 ORDER BY time ASC
ok, i think you want
SELECT COUNT(*) FROM TABLE WHERE id <= YOUR_USER_ID
You can get the rank with this query:
SELECT COUNT(*) + 1
FROM entries entries1
INNER JOIN entries entries2 ON (entries1.id != entries2.id AND entries2.time < entries1.time)
WHERE entries1.id = 4
You just need to count the number of people with a better time, and add 1 (since the first rank is 1).
Unfortunately MySQL lacks windowing functions. Therefore you have emulate them. One way to do it is something like this
SELECT userid,
(
SELECT 1 + COUNT(*)
FROM entries
WHERE time < e.time
) place
FROM entries e
WHERE userid = 1
Sample output:
| USERID | PLACE |
|--------|-------|
| 1 | 2 |
If you need total number of entries in the same query
SELECT userid,
(
SELECT 1 + COUNT(*)
FROM entries
WHERE time < e.time
) place,
(
SELECT COUNT(*)
FROM entries
) total
FROM entries e
WHERE userid = 1;
Sample output:
| USERID | PLACE | TOTAL |
|--------|-------|-------|
| 1 | 2 | 5 |
Here is SQLFiddle demo