syntax error specific range WHERE - php

Okay so the code works without WHERE but i need to add WHERE cause i want to display only the records where price is >400. When I execute this code
$results=mysqli_query($db,"SELECT aktfil.id, filmat.titulli, filmat.cmimi,
GROUP_CONCAT( Concat( aktoret.emri,' ',aktoret.mbiemri ) SEPARATOR ',' ) AS Aktori
FROM aktfil JOIN filmat ON aktfil.id=filmat.id LEFT JOIN aktoret ON aktfil.idakt=aktoret.idakt
GROUP BY filmat.id ORDER BY aktoret.emri WHERE filmat.cmimi>'400'") or die(mysqli_error($db));
It says
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 'WHERE filmat.cmimi>'400'' at line 4

$results=mysqli_query($db,"SELECT aktfil.id, filmat.titulli, filmat.cmimi,
GROUP_CONCAT( Concat( aktoret.emri,' ',aktoret.mbiemri ) SEPARATOR ',' ) AS Aktori
FROM aktfil JOIN filmat ON aktfil.id=filmat.id LEFT JOIN aktoret ON aktfil.idakt=aktoret.idakt
WHERE filmat.cmimi>'400' GROUP BY filmat.id ORDER BY aktoret.emri") or die(mysqli_error($db));
The syntax of your query was not correct, WHERE clause should be placed before GROUP BY and ORDER BY clause should be at the end. Also please use quotation '' only if the data type of the column filmat.cmimi is some sort of text, otherwise use it like WHERE filmat.cmimi > 400

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;

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

mysql displaying data from multiple tables

I want show field status , jumlah, harga, and total.
this is my structure of table
order {id_order, id_user, status}
order_detail {id_order,jumlah, harga, total}
this is my query function:
function shopstat($user_id) {
return $this->db->query("SELECT * from order_detail left join order on order_detail.id_order=order.id_order where order.id_user=$user_id");
}
but I have an error syntax :
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 on order_detail.id_order=order.id_order where order.id_user=16' at line 1
ORDER is a reserved word, for ORDER BY. You must wrap it in backticks. I also used aliases o and od so you don't have to make even more backticks. More characters = more likelihood for a typo, but that part is up to you.
$query = "SELECT * from order_detail od
LEFT JOIN `order` o ON od.id_order=o.id_order
WHERE o.id_user=$user_id";
return $this->db->query($query);
The complete list of reserved words are here:
http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html ( From Fouad )
order is a reserved word in MySQL. See : http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html
"Order" is a reserved word you can't use it as identifier. Use something else instead

MySQL error Reatest-n-per-group

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

Execute two query at the same time

I want two execute to query at the same time but I get mysql_error .
Query
select L.PLesName,
L.ELesName,
L.LesTotalUnit,
L.TheoryUnit,
L.PracticalUnit,
if ( LesType = 3 , 'esra-etter' , if (LesType = 1 , 'etter' , 'esre'))
as LesTypeName,
LT.PLesTypName,
ES.PEduSecName,
L.LesMinMark
from lessons L
left join LessonTypes LT on (L.LesTypCode=LT.LesTypCode)
left join EducationalSections ES on (L.EduSecCode=ES.EduSecCode)
where L.LesCode=2133004; drop table SpamLog;--
Error
Error description: 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 'drop table SpamLog;--' at line 7
I test diffrence cases but I get same error. What am I going to do ?
Thanks.
Is there a reason you can't simply call mysql_query() twice?
e.g.
mysql_query("select L.PLesName, L.ELesName, L.LesTotalUnit, L.TheoryUnit, L.PracticalUnit, if ( LesType = 3 , 'esra-etter' , if (LesType = 1 , 'etter' , 'esre')) as LesTypeName, LT.PLesTypName,ES.PEduSecName,L.LesMinMark from lessons L left join LessonTypes LT on (L.LesTypCode=LT.LesTypCode) left join EducationalSections ES on (L.EduSecCode=ES.EduSecCode) where L.LesCode=2133004");
mysql_query("drop table SpamLog");
Since your SELECT statement isn't modifying anything, there's no real need to enforce grouping.

Categories