MYSQL COUNT return NULL? - php

I have googled my problem but didnt get the answer.
I want to list all of the results of below sql including NULL (when COUNT(review.id) return 0 also) but instead i just got the results of articles of place that only contains review.
$sql = "SELECT tbl_place.id, tbl_place.region_id, tbl_place.subregion_id, tbl_place.title, tbl_place.metalink, tbl_place.img_thumbnail, tbl_place.summary, tbl_place.category1_id, tbl_place.category2_id, tbl_place.category3_id, COUNT(review.id) AS total_review FROM tbl_place
JOIN review ON tbl_place.id = review.place_id
WHERE
tbl_place.category1_id = '32' AND
tbl_place.status = '1' AND
review.rating != '0.00'
GROUP BY tbl_place.id
ORDER BY total_review $by
LIMIT $limit OFFSET $offset";

please use left join for review table instead of join. join is by default inner join so it will take only matched records.

the sql should be :
$sql = "SELECT tbl_place.id,
tbl_place.region_id,
tbl_place.subregion_id,
tbl_place.title,
tbl_place.metalink,
tbl_place.img_thumbnail,
tbl_place.summary,
tbl_place.category1_id,
tbl_place.category2_id,
tbl_place.category3_id,
(SELECT COUNT(*) FROM review WHERE review.rating != '0.00' AND tbl_place.id = review.place_id ) AS total_review
FROM tbl_place WHERE
tbl_place.category1_id = '32' AND
tbl_place.status = '1'
GROUP BY tbl_place.id
ORDER BY total_review $by";
it's working! thx guys!

Related

How to use Join in Codeigniter for update Query

I have given my Codeigniter code below, here i need to update a record using join conditions. I used the below code, But shows error
$condition="a.assignto='0' and a.recstatus='1' and b.location='$location' and '$category' IN(SELECT categoryid FROM `tq_productcategory` where productid=c.productid and recstatus='1')";
$this->db->set($data);
$this->db->where($condition);
$this->db->limit($limit);
$this->db->join('tq_customer b','a.customerid=b.customerid');
$this->db->join('tq_product c','a.productid=c.productid');
$this->db->order_by("a.created_on", "asc");
$this->db->update('tq_customerservicesupport a');
Below is my error Msg
Unknown column 'b.location' in 'where clause'
UPDATE `tq_customerservicesupport` `a` SET `assignto` = '10' WHERE `a`.`assignto` = '0' and `a`.`recstatus` = '1' and `b`.`location` = '3227' and '1' IN(SELECT categoryid FROM `tq_productcategory` where productid = `c`.`productid` and `recstatus` = '1') ORDER BY `a`.`created_on` ASC LIMIT 1
Filename: D:/wamp/www/tooquik/system/database/DB_driver.php
Try this:
$sql = "UPDATE tq_customerservicesupport AS a JOIN tq_customer AS b ON a.customerid = b.customerid JOIN tq_product AS c ON a.productid = c.productid SET $data WHERE $condition ORDER BY a.created_on ASC LIMIT $limit";
$this->db->query($sql);
Make sure that the query is correct cause i just wrote it based on your data and better to check it in phpmyadmin to make sure it works fine with no errors.

SQLite SELECT except where single column has duplicate

How can I ensure that s.user_id and w.stage_id do not have duplicate rows? You can see in the screengrab that user_id 117 occurs in two rows. I want to prevent this - perhaps by only including the latest submission in the query?
My returned data is as follows:
My query:
SELECT DISTINCT r.breakout_id, r.case_id, r.stage_id, r.chart_id, s.stage1_choice,s.user_id,w.stage1,w.user_id
FROM Results as r, Submissions as s, Switch as w
WHERE r.breakout_id = '1' AND s.breakout_id = '1' AND w.breakout_id = '1' AND s.user_id = w.user_id AND w.case_id = r.case_id AND s.case_id = r.case_id AND s.stage1_choice IS NOT NULL ORDER BY w.user_id
Try this:
SELECT r.breakout_id, r.case_id, r.stage_id, r.chart_id,
s.stage1_choice,s.user_id,max(w.stage1),w.user_id
FROM Results as r, Submissions as s, Switch as w
WHERE r.breakout_id = '1' AND s.breakout_id = '1' AND w.breakout_id = '1' AND s.user_id = w.user_id
AND w.case_id = r.case_id AND s.case_id = r.case_id AND
s.stage1_choice IS NOT NULL
GROUP BY r.breakout_id, r.case_id, r.stage_id, r.chart_id,
s.stage1_choice,s.user_id,w.user_id
ORDER BY w.user_id;

SQL query empty field, select ALL option like *

A php search page uses an SQL query to search for properties based on set criteria, in the example below the street "Harrison Road" is chosen:
search.php?city=1&rooms=3&rooms_upper=7&price_lower=55&price_upper=100&streetname=Harrison%20Road
I am looking for a way to leave the streetname blank and still SELECT ALL records as below:
search.php?city=1&rooms=3&rooms_upper=7&price_lower=55&price_upper=100&streetname=
I tried using the ' * ' symbol as with other SQL ocmmands to specify SELECT ALL but it does not work in this instance.
The problem is the search does not display any results while the street option is left blank.
The reason I am looking to run a search with an empty street criteria is because the search.php loads before the user selects a particular street using the drop down option on the page.
I would like it to SEARCH ALL records using the first criteria specified:
rooms, rooms_upper, price_lower, price_upper
The standard search page load has been left with the widest search criteria possible ( 3 < rooms < 7 ) and (£55 < rent < £75) in order to display ALL records before the user narrows the search criteria specifying a particular 'streetname' if desired.
Many Thanks!
Jeanclaude
The full SQL is here:
$sql=mysql_query("SELECT main_table.housenumber, main_table.housenumber, main_table.streetname,
max(main_table.rent) AS reviews_rent, main_table.rooms AS reviews_rooms,main_table.average,
houses.houseID, houses.path, houses.rooms AS houses_rooms,houses.rent AS houses_rent
FROM main_table
LEFT JOIN houses ON main_table.housenumber = houses.housenumber
AND main_table.streetname = houses.streetname
WHERE main_table.streetname='$page_streetname'
AND main_table.city=$city
AND main_table.verify='1'
AND main_table.rooms>='$rooms'
AND main_table.rooms<='$rooms_upper'
AND main_table.rent>=$price_lower
AND main_table.rent<=$price_upper
GROUP BY main_table.housenumber, main_table.streetname
ORDER BY average DESC, houseID DESC, reviewID DESC;");
I want to keep streetname in the WHERE clause, but I don't want to restrict the search if it is left blank.
How about creating the query dynamically.
Set up the base query without the streetname in the WHERE clause but include a sprintf string tag.
Then if $page_streetname has a value add the streetname selection to the query dynamically or if not just add nothing.
$q = "
SELECT main_table.housenumber,
main_table.streetname,
Max(main_table.rent) AS reviews_rent,
main_table.rooms AS reviews_rooms,
main_table.average,
houses.houseid,
houses.path,
houses.rooms AS houses_rooms,
houses.rent AS houses_rent
FROM main_table
LEFT JOIN houses
ON main_table.housenumber = houses.housenumber
AND main_table.streetname = houses.streetname
WHERE %s
main_table.city = $city
AND main_table.verify = '1'
AND main_table.rooms >= '$rooms'
AND main_table.rooms <= '$rooms_upper'
AND main_table.rent >=$ price_lower
AND main_table.rent <=$ price_upper
GROUP BY main_table.housenumber,
main_table.streetname
ORDER BY average DESC,
houseid DESC,
reviewid DESC";
$q = isset($page_streetname)
? sprintf( $q, "main_table.streetname = '$page_streetname' AND " )
: sprintf( $q, '');
That's easily solved with PHP (no need for sprintf()):
<?php
$page_streetname = empty($page_streetname)
? null
: "AND main_table.streetname = '{$page_streetname}'"
;
$result = mysql_query(
"SELECT
main_table.housenumber,
main_table.housenumber,
main_table.streetname,
max(main_table.rent) AS reviews_rent,
main_table.rooms AS reviews_rooms,
main_table.average,
houses.houseID,
houses.path,
houses.rooms AS houses_rooms,
houses.rent AS houses_rent
FROM main_table
LEFT JOIN houses
ON main_table.housenumber = houses.housenumber
AND main_table.streetname = houses.streetname
WHERE
main_table.city = '{$city}'
AND main_table.verify = 1
AND main_table.rooms >= '{$rooms}'
AND main_table.rooms <= '{$rooms_upper}'
AND main_table.rent >= {$price_lower}
AND main_table.rent <= {$price_upper}
{$page_streetname}
GROUP BY
main_table.housenumber,
main_table.streetname
ORDER BY
average DESC,
houseID DESC,
reviewID DESC"
);
?>

Cant set Limit values in query

i am using Codeigniter i am facing an issue while setting the values of "Limit" in a query,Limit is only showing "limit NULL"
Here is the snippet of my code.
SELECT block.loc, owner.name , block.dist_name FROM house INNER JOIN block ON house.block_id = block.block_id INNER JOIN owner ON owner.house_id = house.house_id WHERE
block.dist = ? AND house.status = 5 limit ? , ?
$result = $this->db->query($qry, array($this->getDist(), (int) $this->getLimitStart(), (int) $this->getLimitOffset()));
dump for
(int) $this->getLimitStart() is '0' and (int) $this->getLimitOffset() is '10'
As I understand that you make your own getter setter of the object, the getter which u are providing in you query, is returning NULL is only because you are not using the same setter.
For Example:
if You use it ($this->getLimitOffset()) you have to set it also like this yourObject->setLimitOffset(10). I think it will work for you now.
You should swap the start and offset values like this
SELECT block.loc, owner.name , block.dist_name FROM house INNER JOIN block ON house.block_id = block.block_id INNER JOIN owner ON owner.house_id = house.house_id WHERE
block.dist = ? AND house.status = 5 limit ? , ?
$result = $this->db->query($qry, array($this->getDist(), (int) $this->getLimitOffset(),(int) $this->getLimitStart()));
Becsuse codeigniter active record limit's first parameter is limit abd second is offset.
http://codeigniter.com/user_guide/database/active_record.html
Try this: $this->db->limit($nrecords, $offset);

How can I make an UPDATE query on INNER JOIN using OR

I want to update the values of one column in a table from '0' to '1', if either of four values in columns in another table are '1'. Somehow this doesn't seem to work and I was just wondering if anyone could help me get the code right or find a different way of doing it if it's not possible,
mysql_query("UPDATE members
INNER JOIN forum_banners ON members.id = forum_banners.userid
SET members.beta = '1' WHERE forum_banners.bebeta = '1' OR
forum_banners.bibeta = '1' OR forum_banners.cbeta = '1' OR
forum_banners.wbeta = '1'") or die(mysql_error());
That's what I tried, but it's not working, I suspect because of the OR. I tried having all updatings in different mysql_query bits, but that didn't work either.
You should be able to update from multiple table references. This is untested, but gives you an idea:
UPDATE
members, forum_banners
SET
members.beta = '1'
WHERE
members.id = forum_banners.userid
AND forum_banners.bebeta = '1'
OR forum_banners.bibeta = '1'
OR forum_banners.cbeta = '1'
OR forum_banners.wbeta = '1'
http://dev.mysql.com/doc/refman/5.0/en/update.html
Note "Multi-Table syntax"
Try
UPDATE
m
SET
m.beta = '1'
FROM
members m
INNER JOIN
forum_banners fb
ON m.id = fb.userid
WHERE
fb.bebeta = '1'
OR fb.bibeta = '1'
OR fb.cbeta = '1'
OR fb.wbeta = '1'"
Aliases also help make your syntax a little neater.

Categories