如何将行限制为每天只有1个条目?

时间:2017-06-18 16:50:43

标签: mysql sql

我想将结果限制为每天一行,这是我每天最新的一行:

SELECT * FROM reports WHERE item = :item_id ORDER BY date DESC

每天只有1条记录,每天选择的记录也需要是当天最新的记录。

我真的不知道我应该尝试什么。搜索结果没有给我任何指示。 我正在寻找一个完整的解决方案。 以下是我的表中的示例数据,以JSON格式,仅为单个项目选择:

[{
        "id": "62",
        "user": "7",
        "item": "19333",
        "instant_buy": "798000",
        "instant_sell": "675000",
        "upvotes": "0",
        "downvotes": "0",
        "created": "2017-06-18 14:01:32"
    },
    {
        "id": "61",
        "user": "7",
        "item": "19333",
        "instant_buy": "899999",
        "instant_sell": "735647",
        "upvotes": "0",
        "downvotes": "0",
        "created": "2017-06-18 11:48:25"
    },
    {
        "id": "55",
        "user": "4",
        "item": "19333",
        "instant_buy": "1387166",
        "instant_sell": "1050000",
        "upvotes": "0",
        "downvotes": "0",
        "created": "2017-06-17 12:11:30"
    },
    {
        "id": "38",
        "user": "4",
        "item": "19333",
        "instant_buy": "1850000",
        "instant_sell": "900000",
        "upvotes": "0",
        "downvotes": "0",
        "created": "2017-06-16 15:48:02"
    },
    {
        "id": "36",
        "user": "1",
        "item": "19333",
        "instant_buy": "1529350",
        "instant_sell": "900000",
        "upvotes": "1",
        "downvotes": "0",
        "created": "2017-06-16 14:26:41"
    }]

3 个答案:

答案 0 :(得分:1)

您可以使用与用户的联接以及按用户和日期()分组的最大(已创建)

const path = require('path');
const webpack = require('webpack');
const htmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const publicPath = 'assets/';

const extractSass = new ExtractTextPlugin({
    filename: "[name].[contenthash].css",
    disable: isProd,
    allChunks: true

});
const extractHtml = new htmlWebpackPlugin({
    title: 'Admin Panal',
    minify:{
        collapseWhitespace: false
    },
    hash: true,
    template: './src/index.html' // Load a custom template (ejs by default see the FAQ for details)
});

var isProd  = process.env.NODE_ENV === 'production', // this will return true or false depend on package.json
    cssDev  = ["style-loader","css-loader","sass-loader"],
    cssProd = extractSass.extract({
        use:[{
                loader: "css-loader", // translates SASS into Common CSS
                options: {sourceMap: true, convertToAbsoluteUrls: true } // convertTo.. will resolve images path
            }, {
                loader: "sass-loader"
            }],
        fallback: "style-loader"
    });
var cssConfig = isProd ? cssProd: cssDev;
module.exports={
    devServer: {
        contentBase: path.join(__dirname, "dist"),
        compress: true,
        stats: "errors-only",
        watchContentBase: true,
        hot: true,
        inline: true,
        open: true,
        port: 9000,
        openPage: ''
    },
    devtool: "source-map", // show .map files for better debugging.
    entry: './src/ts/app.ts',
    output:{
        path: path.resolve(__dirname, 'dist'),
        filename: 'app.bundle.js'
    },
    resolve: {
        extensions: ['.js', '.jsx', '.ts', '.tsx', '.scss', '.css']
    },
    module:{
        rules: 
        [{   
            test: /\.scss$/, 
            use: cssConfig
        },{
            test: /\.tsx?$/,
            loader: 'ts-loader',
            exclude: /node_modules/,
        },
        {
            test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2|json|xml|ico)(\?.*)?$/i,
            loader: [
                'file-loader?name='+publicPath+'[name].[ext]',
                {
                    loader: 'image-webpack-loader',
                    options: 
                    {
                        query: 
                        {
                            progressive: true,
                            optimizationLevel: 7,
                            interlaced: false,
                            pngquant:
                            {
                                quality: '65-90',
                                speed: 4
                            }
                        }
                    }
                }
            ]
        }]
    },
    plugins: [ extractHtml, extractSass,
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NamedModulesPlugin()]
}

答案 1 :(得分:0)

您可以在date列上使用SELECT * FROM reports WHERE item = :item_id GROUP BY DATE_FORMAT(date,'%m-%d-%Y') ORDER BY date DESC 。类似于

的东西
var obj = '[{
  "key" : "value"
}]';

答案 2 :(得分:0)

尝试类似的事情:

select reports.* 
from reports inner join 
  (select distinct date(Date), (select ID from reports 
      where date(Date) = date(r1.Date) and item = :item_id
      order by Date desc 
      limit 1) ID 
    from Reports r1 where item = :item_id) s1 
 on reports.id = s1.id

取决于您是否需要日期的第一个或最后一个条目,您应该更改s1子查询的排序