I wanted to query only those items that have a highest likes.
This is the sql query I want to convert into CodeIgnitor's Active Records:
SELECT *, SUM(like) as totalLikes
FROM tbl_like
GROUP BY uploadID
ORDER BY totalLikes DESC
LIMIT 2
CodeIgniter:
public function get_cheezyPic(){
$this->db->select('uploadID, SUM(like) as totalLikes');
$this->db->from('tbl_like');
$this->db->group_by('uploadID');
$this->db->order_by('totalLikes DESC');
$this->db->limit(2);
$query= $this->db->get();
return $query->result_array();}
But when I try to run this code, I've got 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 'like) as totalLikes FROM (tbl_like) GROUP BY uploadID ORDER BY totalLikes ' at line 1
SELECT `uploadID`, SUM(like) as totalLikes FROM (`tbl_like`) GROUP BY `uploadID` ORDER BY `totalLikes` desc LIMIT 2
What's wrong with this code?
Thanks for the help.
This'll work for you. Please note that you were using reserved keyword so it should be enclosed with backtic ``.
public function get_cheezyPic(){
$this->db->select('uploadID, SUM(`like`) as totalLikes',false);
$this->db->from('tbl_like');
$this->db->group_by('uploadID');
$this->db->order_by('totalLikes DESC');
$this->db->limit(2);
$query= $this->db->get();
return $query->result_array();
}
Related
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;
acutally i want get search with multi field so i decided to union query and i write my code following
$this->db->select("id, title, job_type");
$this->db->distinct();
$this->db->from("jobs");
$this->db->like('title',$keyword);
$this->db->get();
$query1 = $this->db->last_query();
$this->db->select("company");
$this->db->distinct();
$this->db->from("employers");
$this->db->join('jobs', 'jobs.userId = employers.userId');
$this->db->or_like('company',$company_name);
$this->db->get();
$query2 = $this->db->last_query();
$query = $this->db->query($query1." UNION ".$query2);
return $query->result();
as above code produce following sql query semes like bellow
SELECT DISTINCT `id`, `title`, `job_type` FROM (`jobs`) WHERE `title`
LIKE '%dfsdf%' UNION SELECT DISTINCT `company` FROM (`employers`) JOIN
`jobs` ON `jobs`.`userId` = `employers`.`userId` WHERE `company` LIKE '%%'
and it shows following 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 'JOIN jobs ON jobs.userId = employers.userId WHERE
company LIKE '%%' at line 5
can anyone help me
First of all, in order to use union the no. of columns and their data types should match in both the queries.
You are trying to do a union between a query which selects three columns with another query which only one column. This will not work.
To know more about mysql unions, you can go through the below link
http://dev.mysql.com/doc/refman/5.0/en/union.html
I need a little assistance. I need this query
SELECT *, STR_TO_DATE( end_date, "%d/%m/%Y" ) AS DATE
FROM `resume_experience`
WHERE `resume_id` =1
ORDER BY DATE DESC
to be written in codeigniter active record format. This is what I wrote.
$result = $this->db->select('*,STR_TO_DATE(end_date,%d/%m/%Y) AS DATE')
->from('resume_experience')
->order_by('DATE', "desc")
->where($where)
->get()
->result_array();
return $result;
It is giving me the following 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 'FROM (resume_experience) WHERE resume_id = '1' ORDER BY DATE desc' at line 2
SELECT *, STR_TO_DATE(end_date, `%d/%m/%Y)` AS DATE FROM (`resume_experience`) WHERE `resume_id` = '1' ORDER BY `DATE` desc
Any help would highly be appreciated.
Thanks and Waiting,
Ahmad
You need to pass second parameter as FALSE in select() so it will not quote the bacticks for STR_TO_DATE(end_date,%d/%m/%Y) AS DATE
$this->db->select("*,STR_TO_DATE(end_date,'%d/%m/%Y') AS DATE",FALSE)
See active record
STR_TO_DATE() requires quotes around the format string:
$this->db->select('*,STR_TO_DATE(end_date,"%d/%m/%Y") AS DATE')
I can't get a variable to work in SQL statement. I can get it to work when I replace (username = $user) with (ID = 11) which is another column from database and a specific row (11), but I want to include a specific row matching $user from column 'username', along with other random results with a limit of $sn.
When using var_dump($user) I know that the variable has a value, but can't see why it doesn't work in SQL statement.
$photo=mysql_query("SELECT A. * FROM (
SELECT DISTINCT * FROM profile_images
WHERE approved='N'
ORDER BY (username = $user) DESC, RAND()
LIMIT $sn)
as A ORDER BY RAND()");
Getting error message: 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 '#googlemail.com) DESC, RAND() LIMIT 9) as A ORDER BY RAND()' at line 4
Any help appreciated.
Assuming $sn holds integer value and don't require escaping,
$photo=mysql_query("SELECT A. * FROM (
SELECT DISTINCT * FROM profile_images
WHERE approved='N'
ORDER BY (username = '".mysql_real_escape_string($user)."') DESC, RAND()
LIMIT $sn)
as A ORDER BY RAND()");
In general, consider using PDO and bind parameters.
i have the following statement
$result = mysql_query("SELECT * from rests ORDER BY name asc WHERE flag = '1' LIMIT 0 , 20");
am getting the following error
Invalid query: 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 flag = '1' LIMIT 0 , 20' at line 1
am not sure where am going wrong :(
$result = mysql_query("SELECT * from rests WHERE flag = '1' ORDER BY name asc LIMIT 0 , 20");
You cannot have the ORDER BY clause before WHERE clause.
Refer to the MySQL select syntax for the correct order of various clauses.
Try this:
SELECT * from rests WHERE flag = '1' ORDER BY name asc LIMIT 0 , 20
Also Ordering in ascending is default, you may drop the asc.
The ORDER BY clause must be after the WHERE clause. That's all it is.
ORDER BY comes after the WHERE and LIMIT at the end.
WHERE goes before ORDER BY.