This question already has an answer here:
Using Google charts with php/mysqli - cannot get it working
(1 answer)
Closed 3 years ago.
I am pondering if there is away that I can count different values from different columns using one SQL Query.
I am making Graphs on my website and I want it to count No values in rows I was thinking to use the following
$sql="SELECT incident.ppe, incident.induction, incident.actions, incident.ssops
FROM incident WHERE ppe = 'No' OR induction = 'No' OR actions = 'No' OR ssops = 'No'
AND YEAR(date) = YEAR(CURDATE())
AND client_id = '$slcustom1'"
The values are Yes and No Values. What my aim is is to echo the value of each Column in the graph values I am using.
var data = google.visualization.arrayToDataTable([
['', ''],
['PPE', 11], // Value 11 should be row counts of all the No's same with all values below
['Induction', 2],
['Actions', 2],
['SSOPs', 2]
Currently I do a SQL query for each value but this is making my code very long and untidy. Any suggestions would be appreciated. Group by will also work but how do I echo each result to the Graph value
You can combine SUM() and IF() functions to count all 'No' in selected rows like this.
SELECT
SUM(IF(incident.ppe = 'No', 1, 0)),
SUM(IF(incident.induction = 'No', 1, 0)),
SUM(IF(incident.actions = 'No', 1, 0)),
SUM(IF(incident.ssops = 'No', 1, 0))
FROM incident
The IF() function will assign value 1 to the rows where the condition is true (where the attribute value is equal to 'No') and 0 to other rows. SUM() will then count a total of all rows. So if there are 5 'No' in ppe from a total of 9 rows the SUM() result will be 1+1+1+1+1+0+0+0+0=5
Related
I have created fc_forum_post table, that one of the columns is is_question when this column equal 1 means the row is question and when the column equal with 0 means the row is an answer.
Now I want to sort my grid view with status = 2 ether question or answer.
As a matter of fact, I want to sort query with another query.
Something like this:
$query = ForumPost::find()
->select([
'forum_post.*',
'waiting' => ForumPost::find()
->select('COUNT(*)')
->where('forum_post.is_question = 0')
->andWhere('forum_post.status = 2')
->andWhere('question_sub = forum_post.id')
])
->joinWith(['category', 'user'])
->where(['forum_post.is_question' => 1])
->orderBy([
'waiting' => SORT_DESC
'forum_post.status' => SORT_DESC
]);
I have tried this query for my gridview. Actually I want to have answer count column an sort with this column.
question title
answer count
title one
4
title two
3
question title = forum_post.is_question = 1
answer count = forum_post.is_question = 0
If you have any question for understanding better my problem, ask me and I will appreciated for help me to solve my problem.
I am currently querying the database to get a count:
$onorder = $db->selectRow("SELECT COUNT(*) as Count FROM store_orders");
The results will be like:
$onorder['Count'];
How do I update the query to find out if ANY of the results counted have a value of 1 for a column named Priority?
You can do:
$onorder = $db->selectRow(
"SELECT COUNT(*) as a, COUNT(IF(Priority=1,1,NULL)) as b FROM store_orders");
and have:
$onorder['a'];
$onorder['b'];
Inside the COUNT you put a condition so it won't count everything:
IF([condition], [result if true], [result if false])
This way it onlyCOUNT()s the ones with value 1 and not the NULLs.
I'm trying to run this sql query as a php activerecord query:
delete from article_categories where
article_id = 10
and
category_id in (1,4,5,6,7)
This statement works fine when I run it from the mysql console.
I tried doing this from php activerecord:
$query["conditions"] = array('article_id = ? and category_id in (?)', 10, "1,4,5,6,7");
ArticleCategory::delete_all($query);
The problem is it is only deleting one record at a time.
I expect all the records that match
article_id == 3
and
category_id == 1 || category_id == 2 || ... || category_id == 5
to be deleted.
However only the first record with fields
article_id = 3, category_id = 1
gets deleted.
What am I typing wrong?
If you want to use "IN (?)" in your where clause, be sure to pass an array as the condition value. Otherwise it would just read IN ("1,2,3,4") in stead of IN (1,2,3,4) (note the double quotes in the former version!).
So, it would look something like this:
$article_id = 1;
$categories = array(1, 2, 3, 4);
ModelName::delete_all(array(
'conditions' => array('article_id = ? and category_id IN (?)', $article_id, $categories)
));
And if you just want to delete the current record, do:
$record->delete();
Be careful not to invoke ->save() afterward, because the record will be saved again.
I'm not really sure if the ID you're sending it to delete can be empty? The documentation shows this for "Massive Update or Delete".
Post::table()->delete(array('id' => array(5, 9, 26, 30));
So, translated in your situation that would be something like
ArticleCategory::delete(array(array('id' => array(1, 4, 5, 6, 7)),
'conditions'=> array('article_id = ?', 10),
));
I'm running this query which is populating my temp table which I then export from. The priceSingle is calculated by simply doubling the fare. However, for some rows, the category is a single fare anyway, so I don't want to double it. The query is actually embedded in a string in PHP, but the SQL looks like:
INSERT INTO temp
SELECT cruises.code AS sailingId, live, 'USD', 'optioncode', 'rateOptionName', '2',
category AS cabinCategory, fare*2 AS priceSingle, fare AS priceDouble,
supplements_usa.adult AS priceAdditional
FROM fares_usa, supplements_usa, cruises
WHERE fares_usa.cruise_id = supplements_usa.cruise_id
AND cruises.id = fares_usa.cruise_id AND cruises.live = 'Y'
How can I avoid doing fare*2 when category equals 5 or 10?
CASE WHEN category = 5 OR category = 10 THEN fare ELSE fare * 2 END
So I have a mysql table which has a id column (sid) that can be dynamically added to. Now I am doing a query to get count of all entries grouped into each of these sid's like so:
SELECT SUM(IF(sid = 71, 1, 0)) as count71,
SUM(IF(sid = 72, 1, 0)) as count72,
SUM(IF(sid = 75, 1, 0)) as count75,
SUM(IF(sid = 81, 1, 0)) as count85
FROM `table`
WHERE userid=44;
Note that all the SUM fields are created dynamically and I don't know what the actual sid's are. Now question is how do I loop through the resultset for each of the counts using PHP's variable variables? I've tried this but doesn't work.
$count_var = "rs['count71']"
$$count_var // holds number of entries where sid is 71;
Thanks!
May I suggest this instead:
select sid, count(*) from `table` where userid = 44 group by sid ;
You could do:
$count_var = "rs['count71']";
echo eval('return $'. $count_var . ';');
but of course #troelskn's answer is probably the best solution for what you want