gpt4 book ai didi

mysql - MySQL如何判断一个人是否超过21岁

转载 作者:行者123 更新时间:2023-11-29 11:11:24 25 4
gpt4 key购买 nike

我正在尝试在 MySQL 中创建一个 View 。我希望它显示用户的电子邮件、名字、出生日期和当前年龄。我只想显示 21 岁以上的用户。

这就是我所拥有的:

CREATE VIEW 'valid_Users' AS 
SELECT Persons.Email AS Email, Persons.FName + ' ' + Persons.LName AS Name, Persons.DOB AS Birth_Date, DATEDIFF(Persons.DOB, NOW()) AS Current_Age'
FROM Persons
WHERE DATEDIFF(Persons.DOB, NOW()) > 21;

问题是Where子句返回一个负数,这显然不是正确的年龄。我如何编辑这段代码来确定一个人的正确年龄?

我知道这些名称还不起作用......但这是一些当前数据的图像。实际的表是“用户”

enter image description here

最佳答案

尝试:

CREATE VIEW 'valid_Users' AS 
SELECT Persons.Email AS Email, Persons.FName + ' ' + Persons.LName AS Name, Persons.DOB AS Birth_Date, DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(), Persons.DOB)), "%Y")+0 AS Current_Age'
FROM Persons
WHERE DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(), Persons.DOB)), "%Y")+0 > 21;

或者:

CREATE VIEW 'valid_Users' AS 
SELECT Persons.Email AS Email, Persons.FName + ' ' + Persons.LName AS Name, Persons.DOB AS Birth_Date, TIMESTAMPDIFF(YEAR, Persons.DOB, NOW()) AS Current_Age'
FROM Persons
WHERE TIMESTAMPDIFF(YEAR, Persons.DOB, NOW()) > 21;

关于mysql - MySQL如何判断一个人是否超过21岁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40494175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com