My sql search syntax implementation - mysql

How can I implement search function:
where sub_menu_name like '%".$kws."%'
into my working one:
SELECT DISTINCT p.id, p.sub_menu_id, p.sub_menu_name, m.image_id, i.file_url, m.default_menu_id, p.restaurant_id, p.status, p.sub_menu_price
FROM sub_sub_menu AS p
INNER JOIN menu AS m ON m.default_menu_id = p.sub_menu_id
OR m.id = p.sub_menu_id
INNER JOIN icon AS i ON i.id = m.image_id
WHERE p.restaurant_id = '" . (int) $_SESSION['uid'] . "' "
Thanks for any help!

SELECT DISTINCT p.id, p.sub_menu_id, p.sub_menu_name, m.image_id, i.file_url, m.default_menu_id, p.restaurant_id, p.status, p.sub_menu_price
FROM sub_sub_menu AS p
INNER JOIN menu AS m ON m.default_menu_id = p.sub_menu_id
OR m.id = p.sub_menu_id
INNER JOIN icon AS i ON i.id = m.image_id
WHERE p.restaurant_id = '" . (int) $_SESSION['uid'] . "'
AND sub_menu_name LIKE '%".$kws."%'
I think this is what you want.
But I wonder why you have ". and ." before and after $kws. It's meant for concatenation so it won't work in SQL. If you want to check for $kws IN a value, the % % are enough.

Related

Inner SELECT can't use `u.id` which is in outer SELECT

I have the following MySQL query:
SELECT
COUNT(b.`id`) AS todayOverdue,
DATE_FORMAT(t.`created_time`, "%Y%m%d") AS days
FROM
`Bill` b
LEFT JOIN `Order` o ON b.`order_id` = o.`id`
LEFT JOIN `Trade` t ON o.`trade_id` = t.`id`
LEFT JOIN `User` u ON b.`user_id` = u.`id`
WHERE
b. `deadline` <= "' . $todayTime . '"
AND b. `deadline` >= "' . $todayDate . '"
AND b.`is_paid` = 0
AND (
SELECT
COUNT(b2.`id`)
FROM
`Bill` b2
WHERE
b2.`deadline` <= "' . $todayTime . '"
AND b2.`user_id` = u.`id`
AND b2.`is_paid` = 0
OR (
b2.`deadline` <= b2.`paid_time`
AND b2.`is_paid` = 1
)
) < 2
GROUP BY
days
Why can't the inner SELECT use u.id which is in outer SELECT?
The inner select is completely independent of the outer select. The u table is being joined in the outer select in ways that are unknown to the inner select.
When you have
AND b2.`user_id` = u.`id`
Which row in u is being compared to which row in b2? The server has no way of knowing, so you need to define a table u2 and join it in the inner select.

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';

SUM() on all rows of a LEFT JOIN?

I've been playing with LEFT JOINs and I'm wondering if it's possible to get the SUM of all the ratings so far from all users in the below query. The below get's me the information on if the logged in user has rated or not, but i want to show total ratings from other users also.
$query = mysql_query("
SELECT com.comment, com.comment_id, r.rate_up, r.rate_down
FROM comments com
LEFT JOIN ratings r
ON com.comment_id = r.comment_id
AND r.user_id = '" . $user_id_var . "'
WHERE page_id = '" . $category_id_var. "'");
I've tried the following but i only get one comment/row returned for some reason.
$query = mysql_query("
SELECT com.comment, com.comment_id,
r.rate_up, r.rate_down
SUM(r.rate_up) AS total_up_ratings,
SUM(r.rate_down) AS total_down_ratings,
FROM comments com
LEFT JOIN ratings r
ON com.comment_id = r.comment_id
AND r.user_id = '" . $user_id_var . "'
WHERE page_id = '" . $category_id_var. "'");
Any help appreciated. Do i need a different kind of JOIN?
Have you tried using GROUP BY page_id on the end of your SQL?
You could do it something like this:
SELECT
com.comment,
com.comment_id,
Total.total_down_ratings,
Total.total_up_ratings
FROM comments com
LEFT JOIN
(
SELECT
SUM(r.rate_up) AS total_up_ratings,
SUM(r.rate_down) AS total_down_ratings,
r.comment_id
FROM
ratings r
GROUP BY
r.comment_id
) AS Total
ON com.comment_id = Total.comment_id
AND r.user_id = '" . $user_id_var . "'
WHERE page_id = '" . $category_id_var. "'"
If you use an aggregation function in SQL (like SUM()) you will need a corresponding GROUP BY clause.
In your case the most likely one would be com.comment_id; this will give you the sum of all ratings per comment_id:
I don't know if you are duplicating comment rows for purpose but if not you can try write subselect for ratings like this:
select comment_id, user_id, sum(rate_up) as sum_rate_up,
sum(rate_down) as sum_rate_down from ratings
group_by comment_id, user_id;
and then include it in your join query:
select com.comment, com.comment_id, r.user_id, r.sum_rate_up,
r.sum_rate_down from comments com
left join (select comment_id, user_id, sum(rate_up) as sum_rate_up,
sum(rate_down) as sum_rate_down from ratings
group_by comment_id, user_id) as r
on com.comment_id = r.comment_id where page_id = '".$category_id_var."'

MySQL COUNT syntax in bigger query

I'm trying to create a query that will return all the jobs published by a specific company, and a count of the total people who applied to this job. the first part works fine - I get all the jobs and everything I need:
$query = "SELECT *,j.job_id as jid, c.name as city_name ".
"FROM jobs j JOIN areas a ON a.area_id = j.job_area ".
"JOIN positions p ON p.position_id = j.job_position ".
"JOIN fields f ON f.id = j.job_field ".
"JOIN cities c ON j.job_city = c.id ".
"JOIN jobTypes jt ON j.job_type = jt.job_id " .
"JOIN companies comp ON j.job_company = comp.company_id ".
"LEFT JOIN jobApplications ja ON ".
"ja.user_id = '".$_SESSION['user_id']."' AND ".
"j.job_id = ja.job_id WHERE j.job_company='$company_id'";
The thing is, that I want to add each result row the number of applicants for the job from the jobApplications table... I tried to add a COUNT column to the query, which works great by itself:
SELECT COUNT(*) FROM jobApplications ja WHERE ja.job_id=j.job_id
when added to the first big query, I didn't manage to make this work even on the syntax level, so i'm not sure if it works at all...
I tried to add the last query to the select area of the main query, but I always get a syntax error right after the 'ja.job_id=j.job_id' in the end of the count query...
Is this even possible ?
I hope the question is clear, I know there are many tables included here...
Thanks for the time and help!
i dont know your PK of jobApplications, but this might work.
$query = "SELECT *,j.job_id as jid, c.name as city_name, COUNT(ja.<primary key>) ".
"FROM jobs j JOIN areas a ON a.area_id = j.job_area ".
"JOIN positions p ON p.position_id = j.job_position ".
"JOIN fields f ON f.id = j.job_field ".
"JOIN cities c ON j.job_city = c.id ".
"JOIN jobTypes jt ON j.job_type = jt.job_id " .
"JOIN companies comp ON j.job_company = comp.company_id ".
"LEFT JOIN jobApplications ja ON ".
"ja.user_id = '".$_SESSION['user_id']."' AND ".
"j.job_id = ja.job_id WHERE j.job_company='$company_id' ".
"GROUP By jid";
You have to use GROUP
SELECT *,j.job_id as jid, c.name as city_name, COUNT(jobApplications.*)
FROM jobs j JOIN areas a ON a.area_id = j.job_area
JOIN positions p ON p.position_id = j.job_position
JOIN fields f ON f.id = j.job_field
JOIN cities c ON j.job_city = c.id
JOIN jobTypes jt ON j.job_type = jt.job_id
JOIN companies comp ON j.job_company = comp.company_id
LEFT JOIN jobApplications ja ON
ja.user_id = '".$_SESSION['user_id']."' AND
j.job_id = ja.job_id WHERE j.job_company='$company_id'
GROUP BY jid
Hope it helps :)
Try it like this:
$query = "SELECT (SELECT COUNT(ja2.job_id) FROM jobApplications ja2 WHERE ja2.job_id=j.job_id group by j.job_id), *,j.job_id as jid, c.name as city_name FROM jobs j JOIN areas a ON a.area_id = j.job_area" .
" JOIN positions p ON p.position_id = j.job_position JOIN fields f ON f.id = j.job_field "
." JOIN cities c ON j.job_city = c.id JOIN jobTypes jt ON j.job_type = jt.job_id " .
"JOIN companies comp ON j.job_company = comp.company_id LEFT JOIN jobApplications ja ON ja.user_id = '".$_SESSION['user_id']."' AND j.job_id = ja.job_id WHERE j.job_company='$company_id'";

mysql syntax how to add a third table to $query

I have code:
$query = "SELECT a.*, c.name as categoryname, c.id as categoryid
FROM #__table_one as a
LEFT JOIN #__table_two c ON c.id = a.catid";
$query .= " WHERE a.published = 1
AND a.access <= {$aid}
AND a.trash = 0
AND c.published =
AND c.access <= {$aid}
AND c.trash = 0";
I would like to add a third table ('__some_table') for the parts of the query where a.publish, a.access and a.trash. In other words, I want these fields to be retrieved from another table, not "#__table_one", but I do not know how to incorporate the #__some_table into the current query
I imagine the JOIN command can help me, but I do not know how to code mysql
//not tested
$query = "SELECT a.*, c.name as categoryname, c.id as categoryid
FROM #__table_one as a
LEFT JOIN #__table_two c ON c.id = a.catid
LEFT JOIN #__table_three d ON d.id = a.some_id";