Add value to New column if condition and group it MySQL - php

I'm trying to get data from database where in 1 table i have a column that divide the price for the product id based on 0 and 1, but on group it by a desired column that value is not populated to the New column.
My question is, how can i add the value to the new column if condition by grouped values:
Database:
Column 1:
id,generic,name,size
1,SS,Test,S
2,SS,Test 2,M
3,SS,Test 3,L
Column 2:
id,product_id,is_valued,price
1,1,0,199
2,1,1,159
3,2,0,599
4,3,0,599
So, in mysql i do:
SELECT column2.price, (CASE WHEN column2.is_valued = '1' THEN column2.price ELSE 'No Valued Price' END) as ValuedPrice FROM column2 LEFT JOIN column1 ON column2.product_id=column1.id GROUP BY column2.generic
OUTPUT:
199,""
The Output i need is: 199,159
so Grouping it by generic column it give me only 1 row, not 2 as expected.
Any help is very appreciated.

SELECT column2.price, (CASE WHEN column2.is_valued = '1'
THEN column2.price ELSE 'No Valued Price' END) as ValuedPrice FROM column2
LEFT JOIN column1 ON column2.product_id=column1.id
GROUP BY column2.generic,column2.price

Related

How to display duplicated results only in MYSQLI?

I want to display only the duplicated results in a PHP page, not their count and there is a condition where certain field can't be empty. What is the mysqli query for displaying each duplicated result and not grouping them so it displays all the duplicates in 1 row rather than displaying each duplicate in a single row?
This is for a reporting panel on full PHP website. I have tried some queries like SELECT col1,col2 FROM table Where col3!='' GROUP BY col2 HAVING COUNT(col1) > 1; But this will display the duplicated results in 1 row, if I have 3 duplicates with same col2 the query returns only one due to the GROUP BY col2 clause, I tried GROUP BY col2,col1 but now HAVING COUNT(col1) > 1 can never be true since it is impossible to have same value of col1.
I expect the output of the query to display each duplicated result in a row and not group them in 1 row only. In other words to display the same 3 duplicated results having same col2 but different other columns values and not display only their 1 result.
All results:
Query I tried:
Try this
SELECT * FROM usersl Where hash!='' AND user_fullName IN (SELECT user_fullName FROM usersl HAVING COUNT(user_fullName) > 1);
This will give you result as you want
I think you should add one more column in group by clause and that column should be Primary ID column then you will got your desired result.
SELECT col1,col2 FROM table Where col3!='' GROUP BY col2,pk_id;
Try this. I think this helps you.
SELECT u1.* FROM usersl AS u1
INNER JOIN (SELECT user_fullName FROM usersl WHERE hash != '' GROUP BY (user_fullName) HAVING COUNT(user_fullName) > 1) AS u2 ON u1.user_fullName = u2.user_fullName
WHERE u1.hash!=''
First find all ID's of these fields:
$ids = $db->queryColumn(
"SELECT GROUP_CONCAT(id SEPARATOR ',') FROM table WHERE col3 != '' GROUP BY col2 HAVING COUNT(col1) > 1"
);
Then make array of ID's:
$idsToFind = [];
foreach ($ids as $idString) {
array_push($idsToFind, array_walk('trim', explode(',', $idString)));
}
Now do another query where you find elements by ID:
$db->queryAll(
'SELECT col1, col2 FROM table WHERE id IN :idArray',
$this->escapeArray($idsToFind)
);

Column count does not match value count at row 1

While deleting, getting error like Column count does not match value count at row 1
Mysql Query:-
DELETE FROM customers WHERE customers.id IN (SELECT cust_id FROM customers_info)
Try this:
DELETE FROM customers WHERE customers.id IN (SELECT GROUP_CONCAT(cust_id) FROM customers_info)

Update multiple columns and rows sql table

I was reading How to update multiple rows with a single query but i have a question i want to know if you can help me.
I want to update multiple columns and rows with a single query but i need them to update like this:
If column1 and column2 (in all rows) == column1 and column2 (in specific row1) then update value in column3 (in all rows). if not equal then value Whatever but if > or < then value Whatever.
Example :
Column 1 ---- Column2 ----- Column3
Row1 2 3
Row2 2 3 (then) Value1
Row3 2 2 (then) Value2
Row4 2 3 (then) Value1
Many thanks
I think what you're saying is that you want to update rows based on matching to the values in a specific row. We can achieve this by doing a left outer join. If the values match then the join succeeds, so 'row1Match' will not be null. If the values don't match then 'row1Match' will be null. We can then use this to drive the update.
If we assume that the table (which I've called MyTable) has an ID column, and that 'Row1' in your example has an ID of 1, then you want something like this:
UPDATE toUpdate
SET Column3 = (CASE WHEN row1Match IS NOT NULL THEN 'Value1' ELSE 'Value2' END)
FROM MyTable toUpdate
LEFT OUTER JOIN MyTable row1Match ON row1Match.Column1 = toUpdate.Column1
AND row1Match.Column2 = toUpdate.Column2
WHERE row1Match.ID = 1

I want to update a column if the row is duplicate

I have the following table named information.
As you see in the previous table there is column named number, this column has a many duplicate values.
What I want is, if the row is duplicate, update its tab cell to 1, otherwise, leave it as it is 0.
The following is what I done.
$query = mysql_query("UPDATE information SET tab='1' WHERE number = (SELECT distinct number FROM information)");
UPDATE information
SET tab = '1'
WHERE number IN (SELECT number
FROM (SELECT number
FROM information
GROUP BY number
HAVING count(*) > 1
) AS temp)
The subquery will return all duplicated number values.
Edit: I've tried it and MySQL shows 1093 error. I have just edited the query according to the solution found here.
I would do this with an update and join:
UPDATE information i join
(select number
from information i
group by number
having count(*) > 1
) idups
on i.number = idups.number
SET i.tab = '1';

Mysql Join Two Queries

I have two queries:
SELECT opr, COUNT(*) FROM table WHERE field = 'YES' GROUP BY opr
SELECT opr, MAX(category) FROM table WHERE field = 'NO' GROUP BY opr
So basically in the first query I am getting the number of transactions a user makes.
In the second query I am getting a category that all of those transactions fall under. I don't want to get all categories for each transaction they made, just the Max of the category field so that I have one entry per operator.
Until now, I have been catching both result sets in separate arrays and the looping through both arrays to get the complete opr->picks->category.
This doesn't always work it sometimes associates the wrong category to the wrong operator.
Is there a way to combine these two queries into one, so that I get the operator and picks, then the MAX(category)? The issue is that the conditions for each query are different on the field column.
SELECT opr,
COUNT(CASE WHEN field = 'YES' THEN 1 END) AS number_of_transactions,
MAX(CASE WHEN field = 'NO' THEN category END) AS category
FROM table
GROUP BY opr ;
The above logic can be attained in one query by using CASE
SELECT opr, COUNT(CASE WHEN `field` = 'YES' THEN opr END) as `count`,
MAX(CASE WHEN `field` = 'NO' THEN category END) as `max`
FROM table GROUP BY opr
Just add it in:
SELECT opr,count(*), MAX(category) FROM table WHERE field = 'YES' GROUP BY opr
You might also be able to drop it from 2 queries to 1 with this:
SELECT opr,count(*), MAX(category), field FROM table GROUP BY opr, field
even faster on the processor, use a sum bool:
select
opr,
sum(field='yes') as transactions,
max(if(field='no',category, null)) as category
from table
group by opr

Categories