简体   繁体   中英

How to subtract date from MySQL datetime where table is specified as a MySQL function.

I'm using the following 'DATE(time)' to get date from datetime and works with no errors.

SELECT * FROM $t WHERE owner = '$sn' && DATE(time) = '$d';

But then when tables are joint I attempt to use "p.DATE(time)" and I get this error.

FUNCTION p.DATE does not exist. Check the 'Function Name Parsing and Resolution'

Here's the full code where I use "p.DATE(time)".

function get_m_stats($con,$sn,$d=NULL,$t=NULL,$o=NULL){
$get_day = mysqli_query($con, "
SELECT m.* , COUNT(p.sid) frequency FROM music m
  JOIN $t p 
  ON p.sid = m.sid 
  WHERE p.sid != '' AND p.owner = '$sn' AND p.DATE(time) = '$d'    
  AND m.perms = 'a'
  GROUP BY m.sid 
  ORDER BY frequency $o 
  LIMIT 1") or die(mysqli_error($con));
$row_day = mysqli_fetch_array($get_day);
echo $row_day['title'];
}

How do I write this 'p.DATE(time)'?

you are assigning alias to function. just put it on column name

 function get_m_stats($con,$sn,$d=NULL,$t=NULL,$o=NULL){
$get_day = mysqli_query($con, "
SELECT m.* , COUNT(p.sid) frequency FROM music m
  JOIN $t p 
  ON p.sid = m.sid 
  WHERE p.sid != '' AND p.owner = '$sn' AND DATE(p.time) = '$d'    
  AND m.perms = 'a'
  GROUP BY m.sid 
  ORDER BY frequency $o 
  LIMIT 1") or die(mysqli_error($con));
$row_day = mysqli_fetch_array($get_day);
echo $row_day['title'];
}

I am sure DATE() is a MySql function - so it cannot exist inside the tabular reference variable p .

Hence the correct way to get your data is getting p.time and then if you want to convert the dateTime to date, use the MySql function DATE .

Hence your query should be :

SELECT m.* , COUNT(p.sid)as frequency 
FROM music m JOIN $t p 
  ON p.sid = m.sid 
  WHERE p.sid != '' 
  AND p.owner = '$sn' 
  AND DATE(p.time) = '$d'    
  AND m.perms = 'a'
  GROUP BY m.sid 
  ORDER BY frequency $o 
  LIMIT 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM