dongyue7796 2017-03-22 17:35
浏览 15
已采纳

使用来自不同表的两个值作为查询中的日期范围

I have the following code:

<?php 
$result = $db->query("select sum(hol_count) as TOTALBOOKEDHOLIDAYS from holidays");
$row = $result->fetch_row();
echo $row[0];
?>

This does a count of hol_count and returns the calculated figure. I have a separate table called 'config' and two columns: 'hol_year_start' and 'hol_year_end' which will set the date range. I am trying include these two values in the query but have encounter a number of errors while trying a number of different ways to achieve this. I have tried to define the two values as variables and then use those variables with BETWEEN to do the count between the who date ranges. I keep getting an error regarding $row = $result->fetch_row(); as well as trying the following:

("select sum(hol_count) as TOTALBOOKEDHOLIDAYS from holidays
BETWEEN
(SELECT hol_year_start FROM config)
AND
(SELECT hol_year_end FROM config) )"

I know this is probably something very simple but I just cant seem to define the two values as variables then use them in the query to retrieve the required data.

What would be the correct way to define the two values from the config table then use them in the query to count hol_count from the holiday table?

  • 写回答

1条回答 默认 最新

  • duanhu7615 2017-03-22 17:52
    关注

    You look you are missing the WHERE in you query. Also the subselects must return only one result. In case you are using MySQL, you would need to set LIMIT 1.

    SELECT SUM(hol_count) AS TOTALBOOKEDHOLIDAYS FROM holidays 
    WHERE date_field_name 
    BETWEEN (SELECT hol_year_start FROM config LIMIT 1) 
    AND (SELECT hol_year_end FROM config LIMIT 1)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。