Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 仅在perl上使用出生年份获取年龄_Mysql_Sql_Perl - Fatal编程技术网

Mysql 仅在perl上使用出生年份获取年龄

Mysql 仅在perl上使用出生年份获取年龄,mysql,sql,perl,Mysql,Sql,Perl,我有一个名为subscribers的mysql表,如下所示: cell |name |year|id|email 10000000|Arabang,GP|F|1981|2|10000000@cs.ub.bw 10000001|Basupang,B.|F|1981|42|10000001@cs.ub.bw 10000002|Bontshitswe,C.|F|1980|36|10000002@cs.ub.bw 10000003|Botsalano,T.|M|1980|42|100

我有一个名为subscribers的mysql表,如下所示:

cell    |name        |year|id|email
10000000|Arabang,GP|F|1981|2|10000000@cs.ub.bw
10000001|Basupang,B.|F|1981|42|10000001@cs.ub.bw
10000002|Bontshitswe,C.|F|1980|36|10000002@cs.ub.bw
10000003|Botsalano,T.|M|1980|42|10000003@cs.ub.bw
10000004|Botshelo,M.|F|1982|14|10000004@cs.ub.bw
10000005|Botshelo,OD|M|1981|10|10000005@cs.ub.bw
10000006|Chabota,M|M|1980|3|10000006@cs.ub.bw
我如何仅使用年份计算年龄。我已经研究过
但我还是不明白怎么做。 *年的数据类型为年*

我的问题是:

my $sth0 =$dbh-> prepare("select *,(  2014 - year)
                          as year  from subscribers 
                          ");

$sth0->execute() or die $DBI::errstr;

我不明白你的问题是什么。你为什么认为这不管用?如果你忽略了一个事实,即你通过这种方式得到的年龄可能太高了一年(取决于此人的生日),那么你的查询是有效的

[test]> create table person (name varchar(20), birth year);
Query OK, 0 rows affected (0.04 sec)

[test]> insert into person values ('Dave', 1962);
Query OK, 1 row affected (0.02 sec)

[test]> select name, year(now()) - birth as age from person;
+------+------+
| name | age  |
+------+------+
| Dave |   52 |
+------+------+
1 row in set (0.00 sec)
(这一年太高了,因为我的生日在九月,现在才是四月。)


注意:我将您硬编码的“2014”改为
year(now())
,因此该查询今后将继续以同样的方式工作。

约翰出生于1964年。他是49岁还是50岁?(提示:没有办法知道)“我如何仅使用年份计算年龄”你不能。如果我将年份的数据类型更改为int,是否可能@andyLook在前面的评论中:如果某人出生于1964年,他是49岁还是50岁?如果他出生于1964年1月,现在是2014年4月,那么他已经50岁了。如果他出生于1964年12月,那么他49岁了。你不能只知道某人的出生年份和当前年份就知道他们的年龄。你只能计算最低年龄或最高年龄,所以从要求中找出更可能的情况并计算出来。谢谢DAVE,我做到了!它工作得很好,不需要大喊大叫。我真的不知道我是怎么帮的。我只是演示了您现有的代码已经在工作。