douchibu7040 2015-11-12 03:18
浏览 52
已采纳

下拉列表选择所有不同的

I want to make a dropdownlist that retrieve the values from the database. The values are the month and the year of the date of user input. The dropdownlist example:-

  • --Select Month--
  • 11-2013
  • 12-2013
  • 03-2015
  • 02-2016

I have done the query to get the result as above:-

SELECT DISTINCT(DATE_FORMAT(DATE(date),'%m-%Y')) AS date FROM tbl ORDER BY DATE_FORMAT(DATE(date),'%Y'), DATE_FORMAT(DATE(date),'%m') ASC

However, I'm quite confused on how to display all of other columns in tbl after selecting the month in the dropdownlist. Say, in the table tbl, there are description and personInCharge column aside the date column. When selecting the month in the dropdownlist, it suppose to retrieve all data that is related with the selected month.

I've tried using these:-

SELECT *, DISTINCT(DATE_FORMAT(DATE(date),'%m-%Y')) AS date FROM tbl ORDER BY DATE_FORMAT(DATE(date),'%Y'), DATE_FORMAT(DATE(date),'%m') ASC

and

SELECT description, personInCharge, DISTINCT(DATE_FORMAT(DATE(date),'%m-%Y')) AS date FROM tbl ORDER BY DATE_FORMAT(DATE(date),'%Y'), DATE_FORMAT(DATE(date),'%m') ASC

and such but all I get is error. Where am I wrong? How am I to complete this? Is it by using join clause? If so, how?

--UPDATE--

I get to list out the details by using this query:-

SELECT *, (DATE_FORMAT(DATE(date),'%m-%Y'))AS month FROM tbl

But, I still don't know how to integrate with the dropdownlist.

  • 写回答

1条回答 默认 最新

  • dongtang6718 2015-11-12 09:38
    关注

    I'm just using these two queries. The first query is for dropdownlist. The second query is to display values from table.

    $sql = mysql_query("SELECT DISTINCT(DATE_FORMAT(DATE(date),'%m-%Y'))
                        AS month
                        FROM tbl
                        ORDER BY DATE_FORMAT(DATE(date),'%Y'), DATE_FORMAT(DATE(date),'%m')
                        ASC");
    
    $query = mysql_query("SELECT *, (DATE_FORMAT(DATE(date),'%m-%Y')) AS month
                                     FROM tbl
                                     WHERE DATE_FORMAT(DATE(date),'%m-%Y')='$date'");
    

    If theres any wrong with my code, lets discuss here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算