How to display rows from MySQL not older than a day? - mysql

I want to display news, for example from yesterday. Here's what my query looks like. Is there any magical line of code which would do that?
$query = '
SELECT a.*,
CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(":", a.id, a.alias)
ELSE a.id END as slug,
CASE WHEN CHAR_LENGTH(b.alias) THEN CONCAT_WS(":", b.id, b.alias)
ELSE b.id END as catslug,
b.title as category_title,
b.alias as categoryalias,
b.params as categoryparams,
u.name AS author
FROM #__content as a
LEFT JOIN #__categories as b ON a.catid = b.id
LEFT JOIN #__users AS u ON u.id = a.created_by
WHERE ' . $where. '
ORDER BY ' . $orderby. '
LIMIT ' . $limit;

The "magical" line of code should be in your WHERE clause.
e.g. if you date is only YYYY-MM-DD
$query .= 'WHERE b.created_date = '.$db->quote( date('Y-m-d', strtotime('-1 day')) ).'
e.g. if your date column is YYYY-MM-DD H:i:s
$query .= 'WHERE DATE_FORMAT(b.created_date,"%Y-%m-%d") = '.$db->quote( date('Y-m-d', strtotime('-1 day')) ).'

Related

Why can't this code run in CodeIgniter?

Query code:
$query = $this->db->query(
'SELECT course.*, AVG(course_review.`rating`) AS `avg_rating`
FROM course
LEFT JOIN course_review ON `course`.`id` = `course_review`.`course_id`
WHERE `course`.`course_category`= ' . $id . '
AND `course`.`approved`= 3
AND `course`.`delete_course` != 1
AND `course`.`unpublish_course` != 1
GROUP BY `course`.`id`
ORDER BY avg_rating DESC');
return $query->result()
Thanks
You can try this Query for your problem :
Query:-
$query = $this->db->query('SELECT
course.id,
AVG(course_review.rating) AS avg_rating
FROM course
LEFT JOIN course_review
ON course.id = course_review.course_id
WHERE course.course_category = ' . $id . '
AND course.approved = 3
AND course.delete_course != 1
AND course.unpublish_course != 1
GROUP BY course.id
ORDER BY avg_rating DESC');
return $query->result();

Dormant users with filter days in prestashop 1.6

I am trying to add below query in $this_select with left join but not working properly
Below is my working query which works fine :
select a.id_customer as id_customer,
a.id_shop,
a.email,
a.lastname,
a.firstname,
max(c.date_add) as last_visit,
IFNULL(max(b.date_add),'1001-01-01 00:00:00') as Last_order_date
from ps_customer a
left join ps_orders b
on a.id_customer = b.id_customer
left join ps_guest g
on a.id_customer = g.id_customer
left join ps_connections c
on g.id_guest = c.id_guest
group by a.id_customer
having to_days(Last_order_date) < to_days(now())- '30'
But my problem is that when I placed below query code in my controller it is not taking the first and the second left join:
$this->_select='
a.id_shop,
a.email,
a.lastname,
a.firstname,
max(c.date_add) as last_visit,
IFNULL(max(b.date_add),"'.$default_date.'") as Last_order_date
';
$this->_join = '
LEFT JOIN `'._DB_PREFIX_.'orders` b ON (a.`id_customer` =b.`id_customer`)';
$this->_join ='left join ps_guest g
on (a.id_customer = g.id_customer)';
$this->_join ='left join ps_connections c
ON ( g.id_guest = c.id_guest)
group by a.id_customer
having to_days(Last_order_date) < to_days(now())- '.$dormant_filter_days.'';
Am I doing anything wrong in the above $this_select or $this_join ??
Bleow is db exception which I get the problem is that I am not seeing my first two joins here ie it is not taking the first two joins
You're overriding the _join value on each call to $this->_join =. You should use $this->_join .= for the second and last join.
$this->_select = '
a.id_shop,
a.email,
a.lastname,
a.firstname,
MAX(c.date_add) AS last_visit,
IFNULL(MAX(b.date_add), "' . $default_date . '") AS Last_order_date';
$this->_join = 'LEFT JOIN `' . _DB_PREFIX_ . 'orders` b
ON (a.`id_customer` = b.`id_customer`)';
$this->_join .= ' LEFT JOIN `' . _DB_PREFIX_ . 'guest` g
ON (a.id_customer = g.id_customer)';
$this->_join .= ' LEFT JOIN `' . _DB_PREFIX_ . 'connections` c
ON (g.id_guest = c.id_guest)';
$this->_group = 'GROUP BY a.id_customer';
$this->_having = 'HAVING TO_DAYS(Last_order_date) < TO_DAYS(NOW()) - ' . $dormant_filter_days;
I tried this way which worked for me :
$this->_select='
a.id_shop,
a.email,
a.lastname,
a.firstname,
a.date_add as registered_date,
g.id_customer as guest_id,
max(c.date_add) as last_visit,
IFNULL(max(b.date_add),"'.$default_date.'") as Last_order_date
';
$this->_join = '
LEFT JOIN `'._DB_PREFIX_.'orders` b ON (a.`id_customer` =b.`id_customer`)
LEFT JOIN ps_guest g on (a.id_customer = g.id_customer)
LEFT JOIN ps_connections c
ON ( g.id_guest = c.id_guest)
';
$this->_where = 'group by a.id_customer having to_days(Last_order_date) < to_days(now())- '.$dormant_filter_days.' AND to_days(a.date_add) < to_days(now())- '.$dormant_filter_days.' ';
$this->_orderBy = 'id_customer';
$this->_orderWay = 'DESC';

CASE Statement in where clause in mysql

I'm trying to fetch data from table where I'm using a CASE condition in the WHERE clause and currently I'm using following query:-
$sQuery = "SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ",
implode(", ", $aColumns))." FROM (select CASE when r.agent_id=24
THEN r.Unit ELSE '--' END AS MyUnit,
CASE when r.agent_id=24 THEN r.landlord_name ELSE '--' END AS landlord_name_new,
r.*,l.loc_name as location,sl.sub_sub_loc as sub_location,
c.category as category,CONCAT(u.first_name, ' ', u.last_name) As agent
from crm_sales r
LEFT JOIN crm_location l ON r.area_location_id=l.loc_id
LEFT JOIN crm_subloc sl ON sl.sub_loc_id = r.sub_area_location_id
LEFT JOIN crm_category c on c.id = r.category_id
LEFT JOIN crm_users u on u.id=r.agent_id
where r.is_active=1 AND r.is_archive=0
AND CASE agent_id WHEN r.agent_id!=24 then r.status=2 else 1=1
group by r.ref) sel
$sWhere
$sOrder
$sLimit
";
Now I want to add one more condition, something like this.
IF(r.agent_id != 24) THEN WHERE r.status=2
EDITED: ADD CASE which i want but error
Fix the case/when clause in your WHERE clause to...
AND CASE WHEN r.agent_id != 24
then r.status = 2
else 1 = 1 end

Limit query results to total results minus 10 results

guys I cant figure this out.. I red a lot but no luck...
I have the following query, and I need to limit it in order to show the total results minus a specific number os results, let's say 10.
I mean, if the query would return 1000 total results, I want it to return 990, and the last 10 results need to be excluded.
Is this possible?
see the query call:
$query = ' SELECT p.*,c.name as name_category,t.name as name_type,cy.name as name_country,s.name as name_state,l.name as name_locality,l.alias as locality_alias,pf.name as name_profile, '
. ' CASE WHEN CHAR_LENGTH(p.alias) THEN CONCAT_WS(":", p.id, p.alias) ELSE p.id END as Pslug,'
. ' CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(":", c.id, c.alias) ELSE c.id END as Cslug,'
. ' CASE WHEN CHAR_LENGTH(cy.alias) THEN CONCAT_WS(":", cy.id, cy.alias) ELSE cy.id END as CYslug,'
. ' CASE WHEN CHAR_LENGTH(s.alias) THEN CONCAT_WS(":", s.id, s.alias) ELSE s.id END as Sslug,'
. ' CASE WHEN CHAR_LENGTH(l.alias) THEN CONCAT_WS(":", l.id, l.alias) ELSE l.id END as Lslug, '
. ' CASE WHEN CHAR_LENGTH(t.alias) THEN CONCAT_WS(":", t.id, t.alias) ELSE t.id END as Tslug '
. ' FROM #__properties_products AS p '
. ' LEFT JOIN #__properties_country AS cy ON cy.id = p.cyid '
. ' LEFT JOIN #__properties_state AS s ON s.id = p.sid '
. ' LEFT JOIN #__properties_locality AS l ON l.id = p.lid '
. ' LEFT JOIN #__properties_profiles AS pf ON pf.mid = p.agent_id '
. ' LEFT JOIN #__properties_category AS c ON c.id = p.cid '
. ' LEFT JOIN #__properties_type AS t ON t.id = p.type '
. ' WHERE p.published = 1 '
.' ORDER BY p.id DESC '
;
If you are using MySQL, then use limit and offset:
limit 10, 9999999
This starts at the 10th record and continues to the end of the data (or to 9999999 rows, technically).
You are ordering by p.id desc, so this will remove the 10 rows with the highest id.
To really do what you seem want (remove the 10 with the lowest id), use a subquery and then sort again after the subquery:
select t.*
from (<most of your query here>
order by p.id asc
limit 10, 999999
) t
order by id desc
To remove the rows with the highest id, just add the limit statement:
$query = 'SELECT p.*,c.name as name_category,t.name as name_type,cy.name as name_country,s.name as name_state,l.name as name_locality,l.alias as locality_alias,pf.name as name_profile, '
. ' CASE WHEN CHAR_LENGTH(p.alias) THEN CONCAT_WS(":", p.id, p.alias) ELSE p.id END as Pslug,'
. ' CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(":", c.id, c.alias) ELSE c.id END as Cslug,'
. ' CASE WHEN CHAR_LENGTH(cy.alias) THEN CONCAT_WS(":", cy.id, cy.alias) ELSE cy.id END as CYslug,'
. ' CASE WHEN CHAR_LENGTH(s.alias) THEN CONCAT_WS(":", s.id, s.alias) ELSE s.id END as Sslug,'
. ' CASE WHEN CHAR_LENGTH(l.alias) THEN CONCAT_WS(":", l.id, l.alias) ELSE l.id END as Lslug, '
. ' CASE WHEN CHAR_LENGTH(t.alias) THEN CONCAT_WS(":", t.id, t.alias) ELSE t.id END as Tslug '
. ' FROM #__properties_products AS p '
. ' LEFT JOIN #__properties_country AS cy ON cy.id = p.cyid '
. ' LEFT JOIN #__properties_state AS s ON s.id = p.sid '
. ' LEFT JOIN #__properties_locality AS l ON l.id = p.lid '
. ' LEFT JOIN #__properties_profiles AS pf ON pf.mid = p.agent_id '
. ' LEFT JOIN #__properties_category AS c ON c.id = p.cid '
. ' LEFT JOIN #__properties_type AS t ON t.id = p.type '
. ' WHERE p.published = 1 '
.' ORDER BY p.id desc limit 10, 999999'
;

MULTIPLE LEFT JOINS AND ORDER BY

I have following query on in my file:
function getItems($ShowFeatured,$OrderBy,$Order,$amountShow,$ShowPropertyID)
{
switch($OrderBy) {
case 0 :
$OrderBy = 'p.id';
break;
case 1 :
$OrderBy = 'p.name';
break;
case 2 :
$OrderBy = 'p.price';
break;
case 3 :
$OrderBy = 'p.hits';
break;
case 4 :
$OrderBy = 'p.refresh_time';
break;
}
switch($Order) {
case 0 :
$Order = 'ASC';
break;
case 1 :
$Order = 'DESC';
break;
}
if($ShowPropertyID){$where= 'WHERE p.id IN ('.$ShowPropertyID.')';}else{$where= 'WHERE p.published = 1 ';}
if($ShowFeatured){$sqlFeatured= ' AND p.featured = 1';}
$db = &JFactory::getDBO();
$query = 'SELECT p.*, i.name as imagename,c.id as key1,c.name as name_category,t.id as key5,t.name as name_type,cy.id as key2,cy.name as name_country,s.id as key3,s.name as name_state,l.id as key4,l.name as name_locality,'
. ' CASE WHEN CHAR_LENGTH(p.alias) THEN CONCAT_WS(":", p.id, p.alias) ELSE p.id END as Pslug,'
. ' CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(":", c.id, c.alias) ELSE c.id END as Cslug,'
. ' CASE WHEN CHAR_LENGTH(cy.alias) THEN CONCAT_WS(":", cy.id, cy.alias) ELSE cy.id END as CYslug,'
. ' CASE WHEN CHAR_LENGTH(s.alias) THEN CONCAT_WS(":", s.id, s.alias) ELSE s.id END as Sslug,'
. ' CASE WHEN CHAR_LENGTH(l.alias) THEN CONCAT_WS(":", l.id, l.alias) ELSE l.id END as Lslug, '
. ' CASE WHEN CHAR_LENGTH(t.alias) THEN CONCAT_WS(":", t.id, t.alias) ELSE t.id END as Tslug '
. ' FROM #__properties_products AS p '
. ' LEFT JOIN #__properties_country AS cy ON cy.id = p.cyid '
. ' LEFT JOIN #__properties_state AS s ON s.id = p.sid '
. ' LEFT JOIN #__properties_locality AS l ON l.id = p.lid '
. ' LEFT JOIN #__properties_category AS c ON c.id = p.cid '
. ' LEFT JOIN #__properties_type AS t ON t.id = p.type '
. ' LEFT JOIN #__properties_images AS i ON p.id = i.parent '
. $where
. $sqlFeatured
. ' GROUP BY p.id '
. ' ORDER BY '.$OrderBy.' '.$Order
. ' LIMIT '.$amountShow;
$db->setQuery($query);
$items = $db->loadObjectList();
// $items = ($items = $db->loadObjectList())?$items:array();
//echo str_replace('#_','jos',$query);
return $items;
Part to get an image is following:
. ' LEFT JOIN #__properties_images AS i ON p.id = i.parent '
This query orders images by i.parent - image that I get I don't like. In my database I have row i.ordering - is it possible to order images by i.ordering on above query?
Thanks.
Something like this for you query I think will do what you want.
$query = 'SELECT p.*, i.name as imagename,c.id as key1,c.name as name_category,t.id as key5,t.name as name_type,cy.id as key2,cy.name as name_country,s.id as key3,s.name as name_state,l.id as key4,l.name as name_locality,'
. ' CASE WHEN CHAR_LENGTH(p.alias) THEN CONCAT_WS(":", p.id, p.alias) ELSE p.id END as Pslug,'
. ' CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(":", c.id, c.alias) ELSE c.id END as Cslug,'
. ' CASE WHEN CHAR_LENGTH(cy.alias) THEN CONCAT_WS(":", cy.id, cy.alias) ELSE cy.id END as CYslug,'
. ' CASE WHEN CHAR_LENGTH(s.alias) THEN CONCAT_WS(":", s.id, s.alias) ELSE s.id END as Sslug,'
. ' CASE WHEN CHAR_LENGTH(l.alias) THEN CONCAT_WS(":", l.id, l.alias) ELSE l.id END as Lslug, '
. ' CASE WHEN CHAR_LENGTH(t.alias) THEN CONCAT_WS(":", t.id, t.alias) ELSE t.id END as Tslug '
. ' FROM #__properties_products AS p '
. ' LEFT JOIN #__properties_country AS cy ON cy.id = p.cyid '
. ' LEFT JOIN #__properties_state AS s ON s.id = p.sid '
. ' LEFT JOIN #__properties_locality AS l ON l.id = p.lid '
. ' LEFT JOIN #__properties_category AS c ON c.id = p.cid '
. ' LEFT JOIN #__properties_type AS t ON t.id = p.type '
. ' LEFT JOIN (SELECT parent, MAX(ordering) AS MostImportant FROM #__properties_images GROUP BY parent) x ON p.id = x.parent '
. ' LEFT JOIN #__properties_images AS i ON x.parent = i.parent AND x.MostImportant = i.ordering '
. $where
. $sqlFeatured
. ' GROUP BY p.id '
. ' ORDER BY '.$OrderBy.' '.$Order
. ' LIMIT '.$amountShow;
You might want to use MIN instead of MAX depending on which way the ordering works