I am working in OpenCart and am trying to change this query. I need to change it so that it returns records from that last year(1 year from now) here is my query
$query = $this->db->query("SELECT count(*) AS total, channel FROM `" . DB_PREFIX . "order` WHERE customer_id = '" . (int)$customer_id . "' && order_status_id IN(" . implode(",", $implode) . ") AND YEAR(date_added) = YEAR(CURDATE()) GROUP BY channel");
I tried changing it to this but it ddidnt work:
$query = $this->db->query("SELECT count(*) AS total, channel FROM `" . DB_PREFIX . "order` WHERE customer_id = '" . (int)$customer_id . "' && order_status_id IN(" . implode(",", $implode) . ") AND date_added BETWEEN CURDATE() - INTERVAL 1 year AND CURDATE()) GROUP BY channel");
Before, consider to sanitize your input data to protect your queries against SQL Injection. Here you can find some content about this topic.
Based on this answer, we can adapt your PHP code to the following:
$query = $this->db->query("SELECT count(*) AS total, channel
FROM `" . DB_PREFIX . "order`
WHERE customer_id = '" . (int)$customer_id . "'
AND order_status_id IN(" . implode(",", $implode) . ")
AND date_added >= DATE_SUB(NOW(),INTERVAL 1 YEAR)
GROUP BY channel");
You got the wrong syntax when calculating the last year.
You can use DATE_SUB.
$query = $this->db->query("SELECT count(*) AS total, channel FROM `" . DB_PREFIX . "order` WHERE customer_id = '" . (int)$customer_id . "' && order_status_id IN(" . implode(",", $implode) . ") AND date_added BETWEEN DATE_SUB(CURDATE(), INTERVAL 1 YEAR) AND CURDATE()) GROUP BY channel");
Related
Tried to combine another query to my existing queries but failed.
Here is the working query without LEFT JOIN
$order_query = $this->db->query("SELECT * FROM " . DB_PREFIX .
"order_items WHERE order_id = '" . (int)$order_id . "'");
Below is the one tried to combined (not working)
$order_query = $this->db->query("SELECT * FROM " . DB_PREFIX .
"order_items WHERE order_id = '" . (int)$order_id . "'" LEFT JOIN .
DB_PREFIX ."item_description WHERE item_id= '" . (int)$item_id . "'"
);
Any idea where did i do wrong ?
You need to construct the join statement correctly. The WHERE clauses come in later.
$prefix = DB_PREFIX;
$sql = "
SELECT oi.* FROM {$prefix}order_items AS oi
LEFT JOIN {$prefix}item_description AS itd
ON oi.item_id = itd.item_id
WHERE oi.order_id = ?
AND itd.item_id = ?
";
$this->db->query($sql, [$order_id, $item_id]);
You can try this code
$order_query = $this->db->query("SELECT oi.* FROM " . DB_PREFIX ."order_items oi
LEFT JOIN ".DB_PREFIX ."item_description des on oi.order_id = des.order_id
WHERE oi.order_id = '" . (int)$order_id . "' and
des.item_id= '" . (int)$item_id . "'");
Hiii i have two diffrent tables
staff_rotas (id,staff_id, startDateTime, endDateTime, rota_minutes, status)
staff_login (id,staff_id, actual_startDateTime,actual_endDateTime,shift_minutes, status);
I want to fetch data from both table where date between e.g (21-11-2016 to 27-11-2016) and output something like this
Timesheet
I have tried following query pls check.
<?php
$staff_data = mysql_query("SELECT * FROM staff_rotas LEFT JOIN staff_login ON
(DATE(staff_rotas.startDateTime) = DATE(staff_login.actual_startDateTime))
WHERE staff_rotas.staff_id = '123' AND DATE_FORMAT(staff_rotas.startDateTime,'%Y-%m-%d')
BETWEEN '" . $date_from . "' AND '" . $date_to . "'
UNION
SELECT * FROM staff_rotas RIGHT JOIN staff_login ON (
DATE(staff_login.actual_startDateTime) = DATE(staff_rotas.startDateTime))
WHERE staff_login.staff_id = '123' AND
DATE_FORMAT(staff_login.actual_startDateTime,'%Y-%m-%d') BETWEEN '" . $date_from . "' AND '" . $date_to . "'
ORDER BY `startDateTime` DESC")
while($data = mysql_fetch_object($staff_data)){
/*html table to show data ......
...
...
*/
}
?>
I'm quite new at this, so I have question about MySQL. I have query in openCart:
$sql = "SELECT cp.category_id AS category_id,
GROUP_CONCAT(cd1.name
ORDER BY cp.level SEPARATOR ' > ') AS name,
c1.parent_id,
c1.sort_order
FROM " . DB_PREFIX . "category_path cp
LEFT JOIN " . DB_PREFIX . "category c1 ON (cp.category_id = c1.category_id)
LEFT JOIN " . DB_PREFIX . "category c2 ON (cp.path_id = c2.category_id)
LEFT JOIN " . DB_PREFIX . "category_description cd1 ON (cp.path_id = cd1.category_id)
LEFT JOIN " . DB_PREFIX . "category_description cd2 ON (cp.category_id = cd2.category_id)
WHERE cd1.language_id = '" . (int)$this->config->get('config_language_id') . "'
AND cd2.language_id = '" . (int)$this->config->get('config_language_id') ."'";
In MySQL I added new column "show_menu" (it can be NULL or 1) and now I want to change my query, that It will return only categories in where show_menu = 1
As I understand I need something like: WHERE show_menu IS 1.
Maybe anyone can help where to put it? because I tried, but no luck...
Use show_menu = 1 or show_menu IS NOT NULL
You just can't use the '=' operator with a null value.
Just add the following to your WHERE clause:
... AND show_menu = 1
Just Like
SELECT * FROM tablename
WHERE conditions_from_your_existing_query
AND show_menu = 1
You can use show_menu = 1 or you can use show_menu is not null. Either way will work.
I have written sql code using group by and order by but in here order by not working can any one help me
SELECT " . DB_PREFIX . "leaderboard_scores.* , SUM(" . DB_PREFIX . "leaderboard_scores.score) as total_score, " . DB_PREFIX . "customer.firstname,
" . DB_PREFIX . "customer.lastname
FROM " . DB_PREFIX . "leaderboard_scores
JOIN " . DB_PREFIX . "customer ON " . DB_PREFIX . "leaderboard_scores.philips_store_id = " . DB_PREFIX . "customer.customer_id
GROUP BY " . DB_PREFIX . "leaderboard_scores.phi_store_id
ORDER BY " . DB_PREFIX . "leaderboard_scores.week DESC
This query work without php errors but given middle rows without giving top weeks. weeks store using times stamp
actual query
SELECT rc_leaderboard_scores.* , SUM(rc_leaderboard_scores.score)
as total_score, rc_customer.firstname, rc_customer.lastname
FROM rc_leaderboard_scores
JOIN rc_customer
ON rc_leaderboard_scores.phi_store_id = rc_customer.customer_id
GROUP BY rc_leaderboard_scores.phi_store_id
ORDER BY rc_leaderboard_scores.week DESC
If you need to return the last record for every customer, you need a JOIN with a subquery that returns MAX(week) for every customer id:
SELECT
rc_leaderboard_scores.*,
m.total_score,
rc_customer.firstname,
rc_customer.lastname
FROM
(SELECT phi_store_id,
MAX(`week`) as max_week,
SUM(score) as total_score,
FROM rc_leaderboard_scores
GROUP BY phi_store_id) m
INNER JOIN
rc_leaderboard_scores
ON rc_leaderboard_scores.phi_store_id = m.phi_store_id
AND rc_leaderboard_scores.`week` = m.max_week
INNER JOIN
rc_customer ON m.phi_store_id = rc_customer.customer_id
the below query works and returns results
$query = "SELECT * FROM table WHERE District = '" . $var . "' ORDER BY Form_Date DESC";
where as if I substitute the word "District" with a variable, it doesn't work
$query = "SELECT * FROM table WHERE '" . $distvar . "' = '" . $var . "' ORDER BY Form_Date DESC";
what is wrong with this one and how can I make it work?
Remove the quotes surrounding the field you are testing for, or replace them with backticks to save you from the mysql parser mistaking it for a potentially reserved word:
$query = "SELECT * FROM `table` WHERE `" . $distvar . "` = '" . $var . "' ORDER BY Form_Date DESC";
try this :
$query = "SELECT * FROM table WHERE `" . $distvar . "` = '" . $var . "' ORDER BY Form_Date DESC";
or
$query = "SELECT * FROM table WHERE " . $distvar . " = '" . $var . "' ORDER BY Form_Date DESC";