PHP SQL Case when value like - php

I am trying to use case when to count the total when the column 'Name' has the value 'apple' in it.
So for example, I have 3 values in my column 'Name'.
hallo (apple), hallo today, hallo (apple)
I am trying to count the result which contains apple and I do not want it in the where part of the statement.
I am trying to accomplish this with the following statement, but I get a SQL error around the like operator. Is what I want possible and if so how do I do it?
SELECT
COUNT(CASE WHEN Name = LIKE '%(apple)%' THEN 1 ELSE null end) AS TotalApples
FROM account WHERE id IN (0,$in)
I expect the result of this query to be 2 as the (apple) is 2 times present in my example.

If you use the LIKE operator, you do not need the = operator
Hence, for your case please change
SELECT
COUNT(CASE WHEN Name = LIKE '%(apple)%' THEN 1 ELSE null end) AS TotalApples
FROM account WHERE id IN (0,$in)
to
SELECT
COUNT(CASE WHEN Name LIKE '%(apple)%' THEN 1 ELSE null end) AS TotalApples
FROM account WHERE id IN (0,$in)
Note: The clause WHERE id IN (0,$in) will only retrieve data where id is 0 or equal to $in. If this is not the case you want then please amend this where clause

Related

How can I UPDATE a column in a record when that information is the result of a GROUP CONCAT in the same table?

I cannot determine if this should be something nested or a JOIN.
Each record has three values from the column value across from their name in the column variable.
I have a successful GROUP CONCAT that combines them into a single text string.
BUT, I need to UPDATE/INSERT the concat value into another variable=>value pair.
Such as this. I want each person to have "cityStateZip" in the value for `cust_abc'.
I believe it's "INSERT" and not update since none of the records have anything in cust_abc yet. But, I'm not quite sure if it shouldn't be UPDATE.
id_member
variable
value
1234
cust_abc
"should show citystatezip"
1234
cust_a
city
1234
cust_b
state
1234
cust_c
zip
I can't get past the error of having the target table being the same in the SELECT FROM.
I was attempting things like:
INSERT INTO smgqg_themes.value (my group concat) WHEN `variable` = "cust_abc"
This is the group concat that works fine to make the string:
SELECT GROUP_CONCAT
( `value`
order by case variable
when 'cust_a' then 1
when 'cust_b' then 2 else 3 end
SEPARATOR '')
output
FROM smfqg_themes
WHERE `id_member` IN (1234, 1235, 1236, etc)
AND `variable` IN ('cust_a', 'cust_b', 'cust_c')
You can INSERT into a the same table you SELECT from in the same SQL statement. I do this all the time.
The syntax is:
INSERT INTO <tablename> (<columns...>)
SELECT ...;
The SELECT must return the same columns that you name in the INSERT clause, in the same order.
If you want an existing row to be updated, but still insert a row if one is missing with the 'cust_abc' varible, then you can use INSERT...ON DUPLICATE KEY UPDATE:
INSERT INTO smgqg_themes (id_member, variable, value)
SELECT id_member, 'cust_abc',
GROUP_CONCAT(`value`
ORDER BY CASE variable
WHEN 'cust_a' THEN 1
WHEN 'cust_b' THEN 2 ELSE 3 END
SEPARATOR '') as v
FROM smgqg_themes
WHERE `id_member` IN (1234, 1235, 1236)
AND `variable` IN ('cust_a', 'cust_b', 'cust_c')
GROUP BY `id_member`
ON DUPLICATE KEY UPDATE value = VALUES(value);
Demo: https://dbfiddle.uk/OPJQ1ZXF

how can i select data and sub query group by

When i select data only show first lot data . but i need all lot data.
Here is my query:
SELECT lot,
(select count(pass) FROM pass_fail_result where pass=0) toatl_fail,
(select count(pass) FROM pass_fail_result where pass=1) toatl_pass FROM pass_fail_result group by lot;
I want to show all pass result like pass=10 and fail=2
The easiest way to do this is via conditional aggregation, where we count or sum CASE expressions which target the failing or passing records:
SELECT
lot,
COUNT(CASE WHEN pass = 0 THEN 1 END) AS toatl_fail,
COUNT(CASE WHEN pass = 1 THEN 1 END) AS toatl_pass
FROM pass_fail_result
GROUP BY
lot;

Add column values to eachother in PHP and MYSQLI

I've been trying to figure out how to count all column values and add them to each other.
What i want to achieve is that my code will calculate all the price values, where check = 1 and add them to eachother as $total.
My goal : So i can divide the total result where check = 1, and check is 0, and subtract them from eachother.
I apologize for my paint skills.
Try this:
SELECT sum(price) as Total from YOUR_TABLE where `check` = 1;
You can try the following:
Step 1: Query all results from the table.
Step 2: Loop through the result.
Step 3: Create two array array1 and array2
Step 4: Inside the loop check condition if (check == 1) stored price value in array1 else if (check == 0) store in array2
Step 5: sum values of array1 and array2 in different variables and finally subtract from each other.
or try the following query:
SELECT SUM(CASE WHEN check='0' THEN price END) as zerossum,
SUM(CASE WHEN check='1' THEN price END) as onessum
FROM tablename
You can get the complete answer using just SQL if you want
SELECT ones - zeros as theTotal
FROM
(
SELECT SUM(CASE WHEN `check`=0 THEN price END) as zeros,
SUM(CASE WHEN `check`=1 THEN price END) as ones
FROM test
) test1;

Doctrine Query Builder Select Count with Case

I have the following sql-statement that I want to transform into doctrine query builder. The goal is to count how many ratings exist with rating value 1 and with rating value 2.
SELECT
COUNT(CASE WHEN rating.rating = 1 THEN rating.rating END) as rat1,
COUNT(CASE WHEN rating.rating = 2 THEN rating.rating END) as rat2
FROM rating
This sql statement is working fine - but when I try to transform it into a Doctrine statement, it does not anymore. When nothing should get counted (because no ratings for this value exist), it returns me a "1" instead of a 0. How can I tell doctrine to simply return a zero when there is nothing to count?
I tried it by removing the "ELSE 0" , but then I get an error that this part is required..
return $qb
->addSelect('COUNT(CASE WHEN r.rating = 1 THEN r.rating ELSE 0 END) as rat_1')
->addSelect('COUNT(CASE WHEN r.rating = 2 THEN r.rating ELSE 0 END) as rat_2')
->getQuery()
->getResult();
Regards,
"sum" is not required - example:
votings 2,2,2,2,2 should return 5 , because the rating with value 2 got voted 5 times.
To count distinct id's in one column depending on the value in another column the answer from Fuzzy Tree does not work in this case.
To count only the distinct values you should give null as a parameter and set it to null like:
->addSelect('COUNT(DISTINCT(CASE WHEN r.rating = 1 THEN rating.rating ELSE :nada END)) as rat_1')
->setParameter(':nada', null)
As vkp mentioned, you can use SUM but instead of summing the rating, sum either a 1 or a 0 to simulate a COUNT
SUM(CASE WHEN r.rating = 1 THEN 1 ELSE 0 END)

SQL count values from same column

Hi I have mysql table named content where i have a column "status" which have 3 values, converted, negotiating and received. now i want to count how many have status received, negotiating, and converted for developing a chart.
here is what i used:
SELECT status,
SUM(CASE WHEN status = 'converted' = 1 THEN 1 ELSE 0 END) AS converted,
SUM(CASE WHEN status = 'negotiating' = 1 THEN 1 ELSE 0 END) AS negotiating,
SUM(CASE WHEN status = 'Received NA' = 1 THEN 1 ELSE 0 END) AS ReceivedNA
FROM content GROUP BY status;
It shows me the result but in a way that i can not use it.
to feed my chart i used this:
$data = array(
array('converted', $converted),
array('negotiating', $negotiating),
array('received', $received)
);
So i guess some thing like this table will solve my problem:
status result
--------------------------- --------
converted 1
negotiating 5
received 4
So can anyone suggest how can modify my sql to get the expected result?
thanks again
Use GROUP By. Try this -
SELECT status, count(status) result FROM content GROUP BY status
To get the distinct count use GROUP BY.
select status,count(1) as result from content GROUP BY status;
Instead of using sum, count is always a better and easier way
EDIT-to answer the comment
The parameter to the COUNT function is an expression that is to be evaluated for each row. The COUNT function returns the number of rows for which the expression evaluates to a non-null value. ( * is a special expression that is not evaluated, it simply returns the number of rows.)
There are two additional modifiers for the expression: ALL and DISTINCT. These determine whether duplicates are discarded. Since ALL is the default, your example is the same as count(ALL 1), which means that duplicates are retained.
Since the expression "1" evaluates to non-null for every row, and since you are not removing duplicates, COUNT(1) should always return the same number as COUNT(*).
Will this work for you?
SELECT status, count(status) FROM content GROUP BY status;

Categories