greatest-n-per-group

FORCE INDEX mySQL …where do I put it?

半世苍凉 提交于 2019-12-03 00:59:11
I have the following mySQL query that works perfectly fine. Except that I need to add a "FORCE INDEX" and I'm unsure on where I have to do this. I tried just about every location and always receive a mySQL error. What am I doing wrong? Here is the original query: $sql_select_recent_items = $db->query("SELECT * FROM (SELECT owner_id, product_id, start_time, price, currency, name, closed, active, approved, deleted, creation_in_progress FROM db_products ORDER BY start_time DESC) as resultstable WHERE resultstable.closed=0 AND resultstable.active=1 AND resultstable.approved=1 AND resultstable

MySQL: how to get x number of results per grouping [duplicate]

非 Y 不嫁゛ 提交于 2019-12-02 21:14:49
Possible Duplicate: mysql: Using LIMIT within GROUP BY to get N results per group? I have a two tables: Items Categories Each item belongs to a category. What I want to do is select 5 items per category but say 20 items in total. SELECT item_id, item_name, items.catid FROM items, categories WHERE items.catid = categories.catid GROUP BY items.catid LIMIT 0,5 //5 per category group Edit: if there are more than 5 items per category - they should be ordered by the item_id (numeric value) Try this query - SELECT item_id, item_name, catid FROM (SELECT t1.*, COUNT(*) cnt FROM items t1 LEFT JOIN items

Selecting best row in each group based on two columns [duplicate]

风格不统一 提交于 2019-12-02 20:54:10
问题 This question already has answers here : SQL select only rows with max value on a column [duplicate] (27 answers) Closed last year . Suppose we have the following table, where each row represents a submission a user made during a programming contest, id is an auto-increment primary key, probid identifies the problem the submission was made to, score is the number of points the submission earned for the problem, and date is the timestamp when the submission was made. Each user can submit as

How can i put IF ELSE CONDITION in Oracle query?

强颜欢笑 提交于 2019-12-02 18:56:21
问题 I have a table, A and B which are shown below, Table A: id idB name faculty B: id name Table B has 2 records as below. SELECT * FROM B; 1, 1, 'First' 2, 2, 'Second' Table A has 8 records as below. SELECT * FROM A; 1, 1, A, IT 2, 1, B, IT 3, 1, C, IT 4, 1, D, Medicine 5, 1, E, Medicine 6, 1, F, Business 7, 1, G, Business 8, 1, H, IT 9, 2, A, Medicine 10, 2, B, Medicine 11, 2, C, Medicine 12, 2, D, Medicine 13, 2, E, Medicine 14, 2, F, Medicine 15, 2, G, Business 16, 2, H, Medicine My question

How do I join 4 tables on mysql select statement?

喜欢而已 提交于 2019-12-02 18:36:17
问题 I have 4 tables in MySQL to join. Example $select = $db->query("SELECT * FROM ads WHERE ad_pic='1' AND ad_status = '1' ORDER BY ad_id DESC LIMIT 0,4"); while ($fetch = $db->fetch($select)) { $iquery = $db->query("SELECT * FROM images WHERE img_ads_id = '" . intval($fetch['ad_id']) . "' AND img_status = '1' LIMIT 1"); $thumb = $db->fetch($iquery); $uselect = $db->query("SELECT * FROM users WHERE user_id = '".intval($fetch['ad_userid'])."' AND user_status = '1' LIMIT 1"); $ufetch = $db->fetch(

SQL max() with inner joins

谁说胖子不能爱 提交于 2019-12-02 18:15:30
问题 I am creating an auction website. Here people can look for items and place bets on them. In the user account area I wish to have a list showing all the items where the user has placed a bid on. Ofcourse each item can have more than one bet from different users so I only wish to show the one with the highest amount of money. This is so that the user can follow all the items on which he placed a bid on, and can track if he is still the one with the highest amount or not. This is how my database

show last comment which just 1 comment per user [duplicate]

梦想的初衷 提交于 2019-12-02 17:59:38
问题 This question already has answers here : Select the 3 most recent records where the values of one column are distinct (8 answers) Closed 3 years ago . I have a normal comments table: | id | comment | date | user | post | status | I want to fetch my 10 last comments with just 1 comment per user, I mean something like this: I have this data: | id | comment | date | user | post | | 1 | text1 | 2016-04-01| 1 | 1 | | 2 | text2 | 2016-04-02| 2 | 1 | | 3 | text3 | 2016-04-03| 1 | 2 | | 4 | text4 |

Complicated MS Access Greatest-N-Per-Group problem

。_饼干妹妹 提交于 2019-12-02 16:40:13
问题 I am looking to combine the following queries into one, where scouting.jumpGate is integer, scouting.astroLoc is a string, scouting.ownerguild is a string and scouting.galaxy is a integer that cross-links to another table (and is my GROUP): Select TOP 3 scouting.jumpGate, scouting.astroLoc, scouting.ownerGuild, scouting.Galaxy FROM scouting WHERE scouting.Galaxy = 1 AND scouting.ownerGuild = 'TEST' ORDER BY scouting.jumpGate DESC, scouting.astroloc DESC; and SELECT TOP 3 scouting.jumpGate,

Get Last Record From Each Month

倖福魔咒の 提交于 2019-12-02 15:52:28
问题 Unfortunately SQL doesn't come to me very easily. I have two tables, a Loan table and a LoanPayments table. LoanPayments Table: ID (Primary Key), LoanID (matches an ID on loan table), PaymentDate, Amount, etc. I need a sql statement that can give me the last payment entered on each month (if there is one). My current statement isn't giving me the results. There is also the problem that sometimes there will be a tie for the greatest date in that month, so I need to be able to deal with that

Query to Return Top Items for Each Distinct Column Value

≡放荡痞女 提交于 2019-12-02 13:39:52
问题 If I have a table with the following fields ID, SomeFK, SomeTime How would I write a query return the latest/top 3 items (based on SomeTime ) for each SomeFK . So, the result might look like SomeFK Sometime 0 2012-07-05 0 2012-07-04 0 2012-07-03 1 2012-07-03 1 2012-07-02 1 2012-07-01 2 2012-07-03 2 2012-07-02 2 2012-07-01 ....etc.... Returning the latest items for a particular SomeFK is easy, but i just can't think how to do it for the above. I also feel it should be dead simple! EDIT: