MySQL error Reatest-n-per-group - php

Help me!
SELECT ma_forum.*, ma_forum_cat.*
FROM ma_forum, ma_forum_cat
JOIN ma_forum_comentarios ON ma_forum_comentarios.not_id = ma_forum.not_id
GROUP BY ma_forum.not_id
WHERE ma_forum.notcat_id=ma_forum_cat.notcat_id AND ma_forum.notcat_id='".$notcat_id."'
AND ma_forum.not_status='Ativo'
ORDER BY MAX(ma_forum_comentarios.comnot_data) DESC
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE ma_forum.notcat_id=ma_forum_cat.notcat_id AND ma_forum.notcat_id='1' ' at line 9"

Your query is in the wrong order; GROUP BY should be after WHERE and before ORDER BY:
SELECT ma_forum.*, ma_forum_cat.*
FROM ma_forum, ma_forum_cat
JOIN ma_forum_comentarios ON ma_forum_comentarios.not_id = ma_forum.not_id
WHERE ma_forum.notcat_id=ma_forum_cat.notcat_id AND ma_forum.notcat_id='".$notcat_id."'
AND ma_forum.not_status='Ativo'
GROUP BY ma_forum.not_id
ORDER BY MAX(ma_forum_comentarios.comnot_data) DESC
I don't know what database you're using, but here's a link to MySQL's SELECT syntax

Related

SQL query error in codeigniter

I tried the code below in phpmyadmin, but received a syntax error:
select * from `reviews_az`
left join `restaurants_az` on `reviews_az`.`restaurant_id` = `restaurants_az`.`id`
where `source` LIKE %YELP% order by `reviews_az`.`id` desc limit 6);
This is the error log:
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '%YELP% order by `reviews_az`.`id` desc limit 6)' at line 1
You are missing single quote in like.
Correct query:
select * from reviews_az left join restaurants_az on reviews_az.restaurant_id = restaurants_az.id where source LIKE '%YELP%' order by reviews_az.id desc limit 6
Use below query. Missing quotes in LIKE.
Change
LIKE '%YELP%'
Query
select * from `reviews_az`
left join `restaurants_az` on `reviews_az`.`restaurant_id` = `restaurants_az`.`id`
where `source` LIKE '%YELP%' order by `reviews_az`.`id` desc limit 6;

SQL query in codeigniter version conflict

I have the Following SQL Query whic runs perfectly in codeigniter version 3
It Shows error in version 2.
$this->db->select('*');
if($cond!=''){
$this->db->where($cond);
}
$this->db->from('(select * from products1 where pr_id in('.implode(",",$condin).') order by pr_id asc) as a ');
$this->db->join(PRICE_TABLE.' as b','a.pr_id=b.pr_id','inner');
$this->db->limit($limit);
$query=$this->db->get();
return $query;
The error is
A Database Error Occurred
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order by pr_id asc) as a) INNER JOIN `pr_product_attr_usa_new` as b ON `a`.`pr_i' at line 2
SELECT * FROM ((select * from products1 where pr_id in(752, `6263`, `542`, `2059)` order by pr_id asc) as a) INNER JOIN `pr_product_attr_usa_new` as b ON `a`.`pr_id`=`b`.`pr_id` WHERE `b`.`pr_stock` = 'yes' LIMIT 6
Filename: D:\XAMPP\htdocs\pr_sites\us\chk\system\database\DB_driver.php
Line Number: 331
The issue is related to the code:
in('.implode(",",$condin).')
which output:
(752, `6263`, `542`, `2059)`
instead of:
(752, `6263`, `542`, `2059`)
The ` symbol is outside the ')'.

MySQL single SELECT showing error in PHP but not in PHPMYADMIN

There are some questions on stackoverflow about it but only with insert, what about that? :
Sql in phpmyadmin:
SELECT logins.user as user,
logins.id as usid,
rozliczenia.godziny as godziny,
rozliczenia.stawka as stawka,
rozliczenia.premia as premia,
rozliczenia.premiainna as premiainna
FROM logins
LEFT JOIN rozliczenia
ON logins.id=rozliczenia.userid
AND DATE(rozliczenia.data) BETWEEN DATE('2015-01-01') AND DATE('2015-01-31')
WHERE logins.user NOT IN ('SUPERUSER', 'agata', 'tomek')
GROUP BY logins.user
ORDER BY logins.id
It works, but in php:
$sql = "SELECT logins.user as user, logins.id as usid, rozliczenia.godziny as godziny, rozliczenia.stawka as stawka, rozliczenia.premia as premia, rozliczenia.premiainna as premiainna FROM logins LEFT JOIN rozliczenia ON logins.id=rozliczenia.userid AND DATE(rozliczenia.data) BETWEEN DATE('2015-10-01') AND DATE('2015-10-31') WHERE logins.user NOT IN ('SUPERUSER', 'agata', 'tomek') GROUP BY logins.user ORDER BY logins.id;";
//The same sql
if(!$result = $polaczenie->query($sql)){
return FALSE;
}
while ($rowV = $result->fetch_array())
{
...
It kinda works, but returns:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '2015-10-01') AND DATE('2015-10-31') WHERE logins.user NOT IN ('SUPERUSER', '' at line 1
What's wrong?
Know all of a sudden more of my scripts return that error. I'm tired of this
EDIT
When I delete
WHERE logins.user NOT IN ('SUPERUSER', 'agata', 'tomek')
GROUP BY logins.user
ORDER BY logins.id
it works, but I need it. If I delete only where or only grouop, it doesn't change anything
Maybe i'm wrong but, your first AND should be after your WHERE.
It was weird, but when i rewrited my code, it worked

Get Average value of from one table for a specific row in another table php

I am trying to do following.
Select a Hospital with id = hid from table named as hospital.
Add a value "overall_rating" to it and get all the ratings and make avg of it from another table named as hrating
here is my query
$statement = $conn->prepare('SELECT hospital.*,(SELECT AVG(hrating.rating_h) FROM hrating WHERE hid = hospital.hid) as overall_rating WHERE hid=:hid LIMIT 1');
Getting this error
{"error":"SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE hid='44' LIMIT 1' at line 1"}
Where am i being wrong.
It appears you don't have a " FROM hospital " bit in your query?
SELECT hospital.*,(SELECT AVG(hrating.rating_h)
FROM hrating
WHERE hid = hospital.hid) as overall_rating
FROM hospital -- this line seems to be missing ??
WHERE hid=:hid LIMIT 1
Try this:
SELECT hospital.*, temp.overall_rating
FROM hospital
LEFT JOIN (SELECT hid AVG(rating_h) as overall_rating
FROM hrating group by hid
) temp
on hid = hospital.hid
WHERE hospital.hid=:hid LIMIT 1

Fatal problem within my MySql queries

I'm having a problem regarding these 3 queries:
select count(*) as rec_count from test_history inner join
test_detail on test_history.history_id = test_detail.test_history_id
where test_history.history_id in ({$_SESSION['history_ids']})
and test_history.user_id = $userID
and
select count(*) as correct_answers from test_history inner join test_detail
on test_history.history_id = test_detail.test_history_id
where test_history.history_id in ({$_SESSION['history_ids']}) and
test_history.user_id = $userID and is_correct = 1
and this one
select * from test_topics inner join
test_topics_link on test_topics.`topic_id` = test_topics_link.`topic_id`
where test_topics_link.test_id in ({$_SESSION['ids_tests']}) and percentage > 0
I'm always getting this error:
You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near ') and
test_history.user_id = 82' at line 3
You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near ') and
test_history.user_id = 82 and is_correct = 1' at line 2
You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near ')' at line 1
You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near ') and percentage > 0' at
line 3
Since $_SESSION['ids_tests'], I suppose, is an array, you should write implode(',',$_SESSION['ids_test']), e.g. in PHP it should be:
$query = "select * from test_topics inner join
test_topics_link on test_topics.`topic_id` = test_topics_link.`topic_id`
where test_topics_link.test_id in (".
implode(',',$_SESSION['ids_tests']).") and percentage > 0";

Categories