SQL在WHERE IN中设置LIMIT

时间:2018-12-13 14:30:13

标签: mysql

我有这样的桌子:

id   user_id   date
1    10        2018-12-13
3    11        2018-11-29
4    12        2018-12-05

我的查询如下:

SELECT * FROM table WHERE IN(10,11,12) AND date > 2018-11-15

现在我从表中获取所有记录> 2018-11-15,但我需要获取第一个用户记录。

我现在得到:

id   user_id   date
1    10        2018-12-13
2    10        2018-12-01
3    11        2018-11-29
4    12        2018-12-05
5    12        2018-12-06

我需要这样:

id   user_id   date
1    10        2018-12-13
3    11        2018-11-29
4    12        2018-12-05

3 个答案:

答案 0 :(得分:0)

一种方法是将表连接到子查询,该子查询查找每个 Map item; List data; Future getdata() async{ http.Response response= await http.get(Uri.encodeFull("https://talaikis.com/api/quotes/")); item=json.decode(response.body); setState(() { data=item["quotes"]; }); debugPrint(data.toString()); } 的最新记录:

    @override
      Widget build(BuildContext context) {
        return new Scaffold(
          drawer: drawerLeft(),
          appBar: AppBar(
              title: Text(
                "IN TIME",
                style: TextStyle(color: Colors.black, fontWeight: FontWeight.w700),
              ),
              backgroundColor: clr,
              elevation: 0.0,
              leading: MaterialButton(
                child: Icon(
                  Icons.view_headline,
                  color: Colors.black,
                ),
                onPressed: () {
                  scaffoldKey.currentState.openDrawer();
                },
              )),
          key: scaffoldKey,
          body: AnimatedContainer(
            padding: EdgeInsets.only(top: 50.0),
            duration: Duration(milliseconds: 1000),
            curve: Curves.ease,
            color: clr,
            child: PageView.builder(
              itemCount: 7, //7days
              onPageChanged: (int page) {
                this.setState(() {
                  Random rnd;
                  rnd = new Random();
                  int r = 0 + rnd.nextInt(_colors.length - 0);
                  clr = _colors[r];

                });
              },
              controller: pageViewController,
              itemBuilder: (BuildContext context, int index) {
                return Padding(
                  padding: const EdgeInsets.only(left: 10.0),
                  child: Stack(
                    children: <Widget>[
                      Container(
                        decoration: new BoxDecoration(
                          borderRadius: BorderRadius.circular(20.0),
                          color: Colors.white,
                        ),
                        height:
                            MediaQuery.of(scaffoldKey.currentContext).size.height -
                                150.0,
                        width:
                            MediaQuery.of(scaffoldKey.currentContext).size.width -
                                20.0,
                        child: Stack(
                          children: <Widget>[
                            Positioned(
                              width: MediaQuery.of(scaffoldKey.currentContext)
                                      .size
                                      .width -
                                  100.0,
                              left: index != currentPage
                                  ? getMappedValue(20.0, 100.0, 160.0, 20.0, pos)
                                  : getMappedValue(20.0, 100.0, 20.0, -120.0, pos),
                              top: 20.0,
                              child: Opacity(
                                opacity: index != currentPage
                                    ? getMappedValue(20.0, 100.0, 0.0, 01.0, pos)
                                    : getMappedValue(20.0, 100.0, 01.0, 00.0, pos),
                                child: Column(
                                  children: <Widget>[
                                    Row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.spaceBetween,
                                      children: <Widget>[
                                        Text(
                                          _days[index],
                                          maxLines: 1,
                                          softWrap: true,
                                          style: TextStyle(
                                              color: Colors.deepOrange,
                                              fontSize: 22.0,
                                              fontWeight: FontWeight.w600),
                                        ),
                                      ],
                                    ),
                                    Padding(
                                      padding: const EdgeInsets.only(top: 15.0),
                                      child: Text(
                                        'Quote for the day',
                                        softWrap: true,
                                        style: TextStyle(
                                            fontSize: 30.0,
                                            fontWeight: FontWeight.w300),
                                      ),
                                    ),

                                  ],
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                );
              },
            ),
          ),
        );
      }
    }

答案 1 :(得分:0)

要获得问题的结果,您需要使用分组并找到最小的id

SELECT t1.id, t1.user_id, t1.date
FROM table t1
INNER JOIN (SELECT MIN(id) AS min_id
            FROM table
            WHERE user_id IN(10, 11, 12)
              AND date > 2018-11-15
            GROUP BY user_id
           ) t2 ON t1.id = t2.id
WHERE t1.user_id IN (10, 11, 12)
  AND date > 2018-11-15;

答案 2 :(得分:0)

也许可以使用NOT EXISTS

SELECT *
FROM yourtable t1
WHERE user_id IN (10, 11, 12)
AND `date` > '2018-11-15'
AND NOT EXISTS
(
  SELECT 1
  FROM yourtable t2
  WHERE t2.user_id = t1.user_id
    AND t2.id < t1.id
    AND t2.`date` > '2018-11-15'
);

在SQL Fiddle here上进行测试