我是mysql的新手,但我有一个数据库,其中有一个Date
列的表格,我想选择"只有"列中同一行上的一年,一个月或一天是我的代码(我用now()函数来存储数据
<?php
$sql = "SELECT * FROM latest_news ORDER BY id DESC LIMIT 1;";
$result = mysqli_query($con, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo $row['Date']; }
}
?>
- &GT;我的数据库表是
CREATE TABLE IF NOT EXISTS `latest_news` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Title` varchar(100) NOT NULL,
`Message` varchar(100) NOT NULL,
`Date` date NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
- &GT;将存储的值保存为
INSERT INTO `latest_news` (`Title`, `Message`, `Date`) VALUES
('Ufunguzi Ofisi ya Masaki', 'Mkurugenzi afungua Ofisi kwa furaha', NOW());
&#34;请帮助我如何才能从行中选择一年? &#34;
答案 0 :(得分:1)
你可以使用
SELECT `ID`,`Title`,`Message`,YEAR(`Date`) as Date FROM latest_news ORDER BY id DESC LIMIT 1
答案 1 :(得分:0)
这个现在有效了,感谢Guys Sloan Thrasher和Vivek Modi,但在这个月我可以按字母顺序而不是数字吗?让它说6我们能得到6月吗?怎么做?
<?php
$sql = "SELECT YEAR(`date`) as Date FROM latest_news ORDER BY id DESC LIMIT 1;";
$result = mysqli_query($con, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo $row['Date'];
}
}
?>