从MySQL中选择特定行并以PHP格式显示

时间:2014-02-06 10:15:14

标签: php mysql sql

我有一个脚本,可以从我的数据库表中导出数据,并通过电子邮件发送数据。这一切都很好,我只是想稍微改变我的查询,以便它只显示我想要显示的特定行。让我们说第2,4,6行......

示例数据库表:

+------+--------+--------+
|Number|Language|Date    |
+------+--------+--------+
|1     |text 1  |20/01/14|
+------+--------+--------+
|2     |text 2  |20/01/14|
+------+--------+--------+
|3     |text 3  |20/01/14|
+------+--------+--------+
|4     |text 4  |20/01/14|
+------+--------+--------+
|5     |text 5  |20/01/14|
+------+--------+--------+
|6     |text 6  |20/01/14|
+------+--------+--------+
|7     |text 7  |20/01/14|
+------+--------+--------+

我目前使用的查询是:

SELECT 
    COUNT(*) as `count`, `lang`, DATE(NOW()) as `month_ending`
FROM
    mydata.table
WHERE
    `date` > DATE_ADD(DATE(NOW()), INTERVAL - 1 MONTH) AND
    `date` < DATE(NOW())
GROUP BY `lang` , DATE(NOW());

我怎么能实现这一点,因为我的表中没有ID,所以我不能指那些行?我还想保留已在查询中的日期排序。

2 个答案:

答案 0 :(得分:1)

添加唯一列并对其进行限制。不要让自己太难;-)。你的新表将是:

+------+------+--------+--------+
|ID    |Number|Language|Date    |
+------+------+--------+--------+
|1     |1     |text 1  |20/01/14|
+------+------+--------+--------+
|2     |2     |text 2  |20/01/14|
+------+------+--------+--------+
|3     |3     |text 3  |20/01/14|
+------+------+--------+--------+
|4     |4     |text 4  |20/01/14|
+------+------+--------+--------+
|5     |5     |text 5  |20/01/14|
+------+------+--------+--------+
|6     |6     |text 6  |20/01/14|
+------+------+--------+--------+
|7     |7     |text 7  |20/01/14|
+------+------+--------+--------+

限制列ID,您就完成了!

WHERE ID = 1 OR ID = 3 OR ID = 5.

答案 1 :(得分:0)

最后我只是采用了这种方法,可能不是最好的但它有效!谢谢!

SELECT count(*) as `count`, `lang`, DATE(NOW()) as `month_ending` FROM mydata.table WHERE `date`>DATE_ADD(DATE(NOW()), INTERVAL -1 MONTH) AND `date`< DATE(NOW()) AND `lang`=("be") OR `lang`=("dk") GROUP BY `lang`, DATE(NOW());