duanfu4446 2014-07-09 09:46
浏览 26
已采纳

如果在SELECT Query中首先匹配,如何跳过其他OR条件?

I am having a trouble with OR condition inside the SELECT.

I want a simple result if one condition is matched and rest OR condition should not be use.

What i want is:

I have some users shared records and i would like to email them the newest items shared on my website.

For me: Newest Items will be least two days older

Like Today is 9th so i would like to pull all records of 7th. but if i didn't get any record of 7th then i would like to pull all record of 6th (3 days older from today). if i didn't get any records on 6th then i would like to pull 1 day older from today.

for all this i have used OR in my SELECT query like this:

SELECT `tg`.* FROM `tblgallery` AS `tg` WHERE (
(tg.added_date BETWEEN '2014-07-07 00:00:00' AND '2014-07-08 00:00:00') OR 
(tg.added_date BETWEEN '2014-07-06 00:00:00' AND '2014-07-07 00:00:00') OR 
(tg.added_date BETWEEN '2014-07-08 00:00:00' AND '2014-07-09 00:00:00') )

And i have records in my database for dates:

2014-07-06
2014-07-07

and when i run this query it gives me all record of both dates.

But I need to pull only record of 2014-07-07 not of both.(I have mentioned above.)

I know i can do this by using multiple Select and i think that will not be a good idea to request to database again and again.

My Question is : How to pull data from database if the first match is true? and skip all data of rest dates?

OR

Is there any other way to do this?

Please Help

  • 写回答

4条回答 默认 最新

  • duanji5116 2014-07-09 12:19
    关注

    Usually one would just work with LIMIT, which is not applicable here, since there might be many rows per day. What I do is quite similar to LIMIT.

    SELECT * FROM (
       SELECT
       tg.*,
       @gn := IF(DATE(tg.added_date) != @prev_date, @gn + 1, @gn) AS my_group_number,
       @prev_date := DATE(tg.added_date)
       FROM tblgallery tg
       , (SELECT @gn := 0, @prev_date := CURDATE()) var_init
       ORDER BY FIELD(DATE(tg.added_date), CURDATE() - INTERVAL 1 DAY, CURDATE() - INTERVAL 3 DAY, CURDATE() - INTERVAL 2 DAY) DESC
    ) sq
    WHERE my_group_number = 1;
    

    Here's how it works.

    With this line

       , (SELECT @gn := 0, @prev_date := CURDATE()) var_init
    

    the variables are initialized.

    Then the ORDER BY is important! The FIELD() function sorts the rows from 2 days ago (gets value 3), to 3 days ago (gets value 2), to 1 day ago (gets value 1). Everything else gets value 0.

    Then in the SELECT clause the order is also important.

    With this line

       @gn := IF(DATE(tg.added_date) != @prev_date, @gn + 1, @gn) AS my_group_number,
    

    the variable @gn is incremented when the date of the current row is different from the date of the previous row.

    With this line

       @prev_date := DATE(tg.added_date)
    

    the date of the current row is assigned to the variable @prev_date. In the line above it still has the value of the previous row.

    Now those entries have a 1 in column my_group_number that have the most recent date in the order

    2 days ago
    3 days ago
    yesterday
    4 days ago
    5 days ago
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 找别人艾特你然后删除的微博
  • ¥15 idea做图书管理系统,要求如下
  • ¥15 最短路径分配法——多路径分配
  • ¥15 SQL server 2022安装程序(英语)无法卸载
  • ¥15 关于#c++#的问题:把一个三位数的素数写在另一个三位数素数的后面
  • ¥15 求一个nao机器人跳舞的程序
  • ¥15 anaconda下载后spyder内无法正常运行
  • ¥20 统计PDF文件指定词语的出现的页码
  • ¥50 分析一个亿级消息接收处理策略的问题?
  • ¥20 uniapp 朋友圈分享单页面自定义操作