如何在php中不显示数据库表到客户端表单的重复值

时间:2016-05-23 03:49:44

标签: php mysql

我有一个数据库表 video_tbl ,如下所示

id       vdate             vname 
1        2016-01-05        dssfsd    
2        2016-02-11        vxc  
3        2016-02-16        vgjhgh      
4        2015-06-12        xcbcvxb       
5        2014-12-03        gjm vcb  

我只想在下拉列表中显示 vdate 年,例如

2016
2015
2014

但我的输出就像这样

2016
2016
2016
2016
2015
2014

在下拉列表中。

<?php
$sql_videodate = mysql_query("SELECT * FROM cf_crane_video ORDER BY cf_vdate",$con);
while($row1 = mysql_fetch_object($sql_videodate))
$vdate = $row1->cf_vdate;
$orderdate = explode('-', $vdate);$month = $orderdate[1];$day   = $orderdate[2];$year  = $orderdate[0];
?>

3 个答案:

答案 0 :(得分:2)

试试这个;)

SQL Fiddle

MySQL 5.6架构设置

CREATE TABLE video_tbl 
    (`id` int, `vdate` datetime, `vname` varchar(7))
;

INSERT INTO video_tbl 
    (`id`, `vdate`, `vname`)
VALUES
    (1, '2016-01-05 00:00:00', 'dssfsd'),
    (2, '2016-02-11 00:00:00', 'vxc'),
    (3, '2016-02-16 00:00:00', 'vgjhgh'),
    (4, '2015-06-12 00:00:00', 'xcbcvxb'),
    (5, '2014-12-03 00:00:00', 'gjm vcb')
;

查询1

SELECT DISTINCT YEAR(`vdate`) year FROM video_tbl ORDER BY `vdate` DESC

<强> Results

| year |
|------|
| 2016 |
| 2015 |
| 2014 |

答案 1 :(得分:0)

尝试将您的SQL分组

GROUP BY YEAR(vdate)

答案 2 :(得分:0)

SELECT DISTINCT 语句用于仅返回不同的(不同的)值

用法:

  

SELECT DISTINCT column_name,column_name FROM table_name;

这应该不会返回重复值