如何从月份为当前月的列中的SQL日期中选择月份

编程入门 行业动态 更新时间:2024-10-27 04:36:02
本文介绍了如何从月份为当前月的列中的SQL日期中选择月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要做出一条选择语句,从客户WHERE返回Voornaam,Tussenvoegsel,Achternaam(我的数据库中有一列名为Geboortedatum,在这里我有一个日期.我需要上面命名的值,其中Geboortedatum是与$currentmonth相同.我已经有一个返回currentmonth的json函数.) 需要明确说明的是:我需要Voornaam,Tussenvoegsel和Achternaam,其中来自表客户的Geboortedatum(DD-MM-YYYY)月与$currentmonth相同.

I need to make a select statement that returns Voornaam, Tussenvoegsel, Achternaam from customer WHERE (I have a column in my database which is called Geboortedatum, in here I have a date. I need al the values named above where Geboortedatum is the same as $currentmonth. I already have a json function which returns the currentmonth.) I am stuck with what to put after WHERE. Just to be clear: I need Voornaam, Tussenvoegsel and Achternaam where the month of Geboortedatum(DD-MM-YYYY) from my table customer is the same as $currentmonth.

$conn = new mysqli("localhost", "root", "Habt2002", "fca"); if ($_POST['key'] == 'bijnaJarig') { $currentmonth = $conn->real_escape_string($_POST['currentmonth']); $sql = $conn->query("SELECT Voornaam, Tussenvoegsel, Achternaam, Telefoonnummer, Email FROM customer WHERE ??????? "); $data = $sql->fetch_array(); $jsonArray = array( 'voornaam' => $data['Voornaam'], 'tussenvoegsel' => $data['Tussenvoegsel'], 'achternaam' => $data['Achternaam'], 'telefoonnummer' => $data['Telefoonnummer'], 'emailbijnajarig' => $data['Email'] ); exit(json_encode($jsonArray)); }

推荐答案

使用MONTH()和CURDATE() MySQL函数,您可以执行

Using the MONTH() and CURDATE() MySQL functions you can do

WHERE MONTH(Geboortedatum) = MONTH(CURDATE())

您可能还想添加YEAR()支票

AND YEAR(Geboortedatum) = YEAR(CURDATE())

除非您希望获得本月中所有年份的所有数据

unless you want all data for this month over multiple years

您仅从结果集中获取一行,您要么需要遍历结果集中的n行,要么使用fetch_all()方法.在这种情况下,fetch_all()似乎是最简单的方法.

You are only fetching ONE row from the resultset, you either need to loop over the n rows in the resultset OR use the fetch_all() method. In this case the fetch_all() seems like the simplest approach.

$conn = new mysqli("localhost", "root", "xxxx", "fca"); if (!$conn) { echo "Error: Unable to connect to MySQL." . PHP_EOL; echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL; echo "Debugging error: " . mysqli_connect_error() . PHP_EOL; exit; } if ($_POST['key'] == 'bijnaJarig') { $sql = $conn->query("SELECT Voornaam, Tussenvoegsel, Achternaam, Telefoonnummer, Email FROM customer WHERE MONTH(Geboortedatum) = MONTH(CURDATE())"); $all_rows= $sql->fetch_all(); echo json_encode($all_rows); }

更多推荐

如何从月份为当前月的列中的SQL日期中选择月份

本文发布于:2023-06-02 07:59:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/440293.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!