sql-order-by

MySQL Rating With Weight

◇◆丶佛笑我妖孽 提交于 2019-12-08 02:29:15
问题 I want to create a rating with weight depending on number of votes. So, 1 voting with 5 can't be better than 4 votings with 4. I found this math form: bayesian = ( (avg_num_votes * avg_rating) + (this_num_votes * this_rating) ) / (avg_num_votes + this_num_votes) How can I make a MySQL SELECT to get the ID of the best rating image. I got a table for IMAGE, and a table for VOTING VOTING: id imageID totalVotes avgVote I think I got to do this with SELECT in SELECT, but how? 回答1: A first step is

Retain ordering from IN subquery

删除回忆录丶 提交于 2019-12-08 01:26:06
问题 this should be an easy one, but I can't seem to get mysql to play ball. I'm trying to build a list of projects that a user most recently voted for, to be used on that users profile page. I have table of votes, containing uid(INT), project(INT), timestamp(INT) And a table of projects whose id field matches the project field in the votes table. My initial query was SELECT * FROM projects WHERE id IN(SELECT project FROM votes WHERE uid=x ORDER BY timestamp DESC); This gives a correct list, but

MySQL GROUP BY / ORDER BY issue with flat messages table / threads

纵饮孤独 提交于 2019-12-08 00:23:24
问题 Ok, I'm trying to base something similar off of this, but not quite getting it nailed: GROUP BY and ORDER BY Basically, wanting a query to find the latest messages in each 'thread' between the current logged-in user and any other users, but via a flat (non-'threaded') table of messages: messages { id, from_uid, to_uid, message_text, time_added } Assuming the current user's uid is '1', and the latest message in each 'thread' could either be from that user, or to that user (the other party

ORDER BY for currency values

独自空忆成欢 提交于 2019-12-08 00:01:35
问题 I have a prize_value column in one of the MySQL tables and now I need to do sorting based on this. This column is actually VARCHAR and has currency symbols attached with it. But not for all the values. The currency symbol can be USD(dollar sign), POUND or INR (Rupee Symbol). So currently the order by is not working properly. How can I fix this without removing the currency symbol manually ? Here are some sample values in the column: .50 £10 £100 $15 $20 £25 £50 10 ₹30 回答1: You need two

Join two tables, then Order By date, BUT combining both tables

我们两清 提交于 2019-12-07 23:47:56
问题 Alright, I'm trying to figure out why I can't understand how to do this well... I have two tables: invoices : id userID amount date payments : id userID amount date So, the goal here is to join both tables, where the userID matches whatever I want it to be - and then return everything ordered by date (most recent at the top). However, because there is a date field in each of the tables, I'm not sure how MySQL will handle things... will is sort by both dates automatically? Here's what I was

MySQL ORDER BY AVG() DESC not working when certain columns are included

人走茶凉 提交于 2019-12-07 21:15:19
问题 I'm doing a query to return all the rows in table1, along with their average rating from table2: SELECT `table1`.`description`, AVG( `table2`.`rating` ) AS avg_rating FROM `table1` LEFT JOIN `table2` ON ( `table2`.`botid` = `table1`.`id` ) GROUP BY `table1`.`id` ORDER BY avg_rating DESC The problem is that even though I specify DESC , the results are being returned ASC : +-------------+------------+ | description | avg_rating | +-------------+------------+ | test2 | 1.0000 | | test3 | 3.0000

Building a snapshot table from audit records

只愿长相守 提交于 2019-12-07 20:40:35
问题 I have a Customer table with the following structure. CustomerId Name Address Phone 1 Joe 123 Main NULL I also have an Audit table that tracks changes to the Customer table. Id Entity EntityId Field OldValue NewValue Type AuditDate 1 Customer 1 Name NULL Joe Add 2016-01-01 2 Customer 1 Phone NULL 567-54-3332 Add 2016-01-01 3 Customer 1 Address NULL 456 Centre Add 2016-01-01 4 Customer 1 Address 456 Centre 123 Main Edit 2016-01-02 5 Customer 1 Phone 567-54-3332 843-43-1230 Edit 2016-01-03 6

sql query not working with order by

◇◆丶佛笑我妖孽 提交于 2019-12-07 18:01:37
问题 Here is my original query that works Select * FROM story st, sentences s, speaker sp WHERE (st.lesson_id = '1' AND st.speaker_id = sp.speaker_id AND st.sentence_id = s.sentence_id) When I try to add a Order By it breaks down. Select * FROM story st, sentences s, speaker sp WHERE (st.lesson_id = '1' AND st.speaker_id = sp.speaker_id AND st.sentence_id = s.sentence_id) ORDER BY st.story_in_lesson_id ASC Can't figure out why it is breaking. EDIT: Here is the error I get Fatal error: Call to a

Sort longtext as int in SQL

空扰寡人 提交于 2019-12-07 17:44:09
问题 I have table in MySQL database where one column type is longtext and there are stored numbers. I need to get content from table sorted by numbers in that column. SELECT * FROM wp_postmeta WHERE meta_key = 'rating_avg' ORDER BY meta_value With this query sorting is not proper and looks like: 0 1.6 10 5 but I need like this: 10 5 1.6 0 I may not change column type, because this column have many different types of data. Is there any possibility to change column type temporary in SQL query? 回答1:

Negative limit offset in mysql

。_饼干妹妹 提交于 2019-12-07 14:56:02
问题 I'm creating a high score server and one of the needed features is being able to retrieve high scores around the users current score. I currently have the following: SELECT * FROM highscores WHERE score >= ( SELECT score FROM highscores WHERE userID = someID ) ORDER BY score, updated ASC LIMIT -9, 19 The only problem here is that the offset parameter of LIMIT can't be negative, otherwise I believe this would work dandy. So in conclusion, is there any trick / way to supply a negative offset to