简体   繁体   English

怎么写这个mysql查询?

[英]How write this mysql query?

I'm so stuck on this and would really appreciate any help. 我对此非常执着,非常感谢您的帮助。 I have a form with some select options and I am trying to get it to query some date rows in my database for entries that meet tomorrow, next week and next month. 我有一个带有一些选择选项的表单,我试图让它查询数据库中的某些日期行,以查找明天,下周和下个月遇到的条目。 I have gotten the mysql commands to work fine in php admin but I cant figure out how to get them to work within my form in php. 我已经获得了mysql命令,可以在php admin中正常工作,但是我无法弄清楚如何使它们在php的表单中工作。 I should add that the variables selected get put into an array, and I just can't figure out where and how to write the MYSQL query. 我应该补充一点,将选择的变量放入数组中,但是我无法弄清楚在哪里以及如何编写MYSQL查询。

The mysql commands I want to use are: 我要使用的mysql命令是:

**NEXT WEEK**
SELECT start FROM  `tablename` 
WHERE START = DATE_ADD( CURDATE( ) , INTERVAL( 9 - IF( DAYOFWEEK( CURDATE( ) ) =1, 8, DAYOFWEEK( CURDATE( ) ) ) ) DAY ) 

**NEXT MONTH**
SELECT start FROM `tablename` WHERE start BETWEEN DATE_SUB(LAST_DAY(DATE_ADD(NOW(), INTERVAL 1 MONTH)),INTERVAL DAY(LAST_DAY(DATE_ADD(NOW(), INTERVAL 1 MONTH)))-1 DAY) AND LAST_DAY(DATE_ADD(NOW(), INTERVAL 1 MONTH))

Here is the form: 形式如下:

<select id="start" name="start" class="form-control">
<option value="" disabled selected>When</option>
<option value="<?php echo date("Y-m-d");?>">Today</option>
<option value="<?php ------what goes here?---------?>">Tomorrow</option>
<option value="<?php-----what goes here?-----------?>">Next Month</option>
</select>

Here is the problem code: 这是问题代码:

$whereClauses = array(); 
if (! empty($_GET['start'])) $whereClauses[] ="(start='".mysqli_real_escape_string($connection,$_GET['start'])."'";

I'd suggest you don't put dates or long strings as your values to your select form. 我建议您不要将日期或长字符串作为您选择的表单的值。 It's just going to become frustrating to debug. 调试变得令人沮丧。 Instead, use a simple key such as: 而是使用简单的密钥,例如:

<select id="start" name="start" class="form-control">
<option value="" disabled selected>When</option>
<option value="today">Today</option>
<option value="tomorrow">Tomorrow</option>
<option value="next_month">Next Month</option>
</select>

And now, you can simple do in your PHP: 现在,您可以在PHP中简单地进行操作:

$whereClauses = array(); 
if (!empty($_GET['start']))
    if ($_GET['start'] == "today") {
        $whereClause[] = "START = CURDATE()";
    }
    else if ($_GET['start'] == "tomorrow") {
        $whereClause[] = "START = DATE_ADD( CURDATE( ) , INTERVAL( 9 - IF( DAYOFWEEK( CURDATE( ) ) =1, 8, DAYOFWEEK( CURDATE( ) ) ) ) DAY )";   
    }
    else if ($_GET['start'] == "next_month") {
        $whereClause[] = "start BETWEEN DATE_SUB(LAST_DAY(DATE_ADD(NOW(), INTERVAL 1 MONTH)),INTERVAL DAY(LAST_DAY(DATE_ADD(NOW(), INTERVAL 1 MONTH)))-1 DAY) AND LAST_DAY(DATE_ADD(NOW(), INTERVAL 1 MONTH))";
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM