Get the place for the user entries [closed] - php

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

Related

Is there a way to count repeated strings in records from a MySQL Table? [closed]

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 last month.
Improve this question
I want to know if there is a way to count repeated strings in diferent records in a MySQL table, for instance, given a table like this one:
+----+-----------------------+
| id | data |
+----+-----------------------+
| 1 | this is here |
+----+-----------------------+
| 2 | this would be nice |
+----+-----------------------+
| 3 | this was here |
+----+-----------------------+
| 4 | this needs to be said |
+----+-----------------------+
I'd like the following result:
Count: this(4), here(2)
This is what I've been looking for, but no luck until now.
You can start with this:
SELECT id, data, SUBSTRING_INDEX(SUBSTRING_INDEX(data,' ',x.x),' ',-1) as Y
FROM test
CROSS JOIN (select 1 as x union select 2 union select 3 union select 4) x
ORDER BY id, x.x
see: DBFIDDLE
output:
id
data
Y
1
this is here
this
1
this is here
is
1
this is here
here
1
this is here
here
2
this would be nice
this
2
this would be nice
would
2
this would be nice
be
2
this would be nice
nice
3
this was here
this
3
this was here
was
3
this was here
here
3
this was here
here
4
this needs to be said
this
4
this needs to be said
needs
4
this needs to be said
to
4
this needs to be said
be
Things left to be done:
Check If you have more, of less than, 4 words. Currently when you only have 3 word the last word is repeated, and when having more than 5 words they are ignored.
Count the stuff, but that is basic SQL stuff using count(*)
BONUS: Getting the number of words in a string:
SELECT
id,
data,
length(data)-length(replace(data,' ',''))+1 as NrOfWords
from test;
output:
id
data
NrOfWords
1
this is here
3
2
this would be nice
4
3
this was here
3
4
this needs to be said
5

How do I order by the sum of two rows, in MySQL, for each user [closed]

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 6 years ago.
Improve this question
I have a table that is Scoreboard.
userID|userName|weekID|pickID|Score
--------------------------------------------
1 |Bob | 1 | Den | 10
1 |Bob | 2 | Clev | 20
2 |Adam | 1 | Car | 12
2 |Adam | 2 | Den | 30
I would like to sort the table by the sum of all of the weeks
My desired result would be this.
userID|userName|weekID|pickID|Score
--------------------------------------------
2 |Adam | 1 | Car | 12
2 |Adam | 2 | Den | 30
1 |Bob | 1 | Den | 10
1 |Bob | 2 | Clev | 20
Adams entries are on top because the sum of his score for Week 1 and Week 2
is the highest. The whole table needs to be sorted this way, so while Im spitting out the HTML on a loop, the rows will be sorted in order.
You need to get the aggregate score per user and sort by that total in descending order, and optionally one or more other columns as well. I assume weekID is a good secondary sort.
You can JOIN the table to itself based on userID, GROUP BY the main table's userID and weekID, and SUM the JOINed table's Score column in order to be able to ORDER correctly.
SELECT s.userid, s.username, s.weekid, s.pickid, s.score, SUM(a.score) totalscore
FROM scoreboard s
INNER JOIN scoreboard a ON a.userid = s.userid
GROUP BY s.userid, s.weekid
ORDER BY totalscore DESC, s.weekid;
If you don't need the total in the data returned you can put the SUM directly in the ORDER BY clause.
SELECT s.userid, s.username, s.weekid, s.pickid, s.score
FROM scoreboard s
INNER JOIN scoreboard a ON a.userid = s.userid
GROUP BY s.userid, s.weekid
ORDER BY SUM(a.score) DESC, s.weekid;
I assume your data is more normalized than this, so adjust accordingly.
I also imagine that you plan on operating for more than just a single season, so you may eventually need to filter on that as well.
The "GROUP BY" ist the key. The following should work:
SELECT userID, SUM(Score) as TotalScore
FROM teh_table
GROUP BY userID
ORDER BY TotalScore DESC;

Customers without orders [closed]

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.

Zend db Mysql sorting by count [closed]

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();

select an entry with specified id, and also one entry before and after with the same foreign key value [closed]

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 a table which looks like this one:
| id | fk_book | name |
-----------------------
| 1 | 2 | test1|
| 3 | 2 | test3|
| 6 | 3 | notes|
| 7 | 2 | test2|
No I want to get the entry with the id 3. select * from test where id=3 AND fk_book=1;
but is there also a way to get the item with id 1 and 7?
and I dont know the ids of the other entries
thanks
I think you need this:
select * from test where id=3 AND fk_book = 2
union all
select * from test where id < 3 AND fk_book = 2 order by id desc limit 1
union all
select * from test where id > 3 AND fk_book = 2 order by id asc limit 1
Try following sql query:
select * from test where id in(1,3,7);

Categories