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
Related
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)
);
I have a few many rows in mysql table.I want to get the values sorted like rows with value 1 in first row rows with value 2 in second row and so on . My table look like this,
id Columnn1 Column2 name
1 1 1 a
2 2 2 b
3 2 3 c
4 3 2 d
5 3 2 e
I want the result as
a in first row
b,c in second row
d,e in third row
that is order by Columnn1
try this:
select group_concat(name) as res from tbl_name order by column1 group by column1;
Use below query
select id,column1,column2,group_concat(name) from table_name
group by column2 order by column2
use GROUP_CONCAT(name) function returns a string with concatenated and separated by comma non-NULL value from a group.
select group_concat(name) from tbl_name order by column1 group by column1;
We have two tables with same structure (MSSQL)
We want to compare
(ID) 122 from table1
with
highest ID 122 from table2
If there is one or more differences, we want to show the name of the column along with the data it holds from both tables.
I do not want to list all the columns out, as there is over 150
Something like (psuedo): -
Select * from table1
Select * from table2
Compare table1 (column by column) with table2 (column by column)
Show changes : -
'Name' WAS 'John' NOW 'James' CHANGED 3/11/2016 3.45pm
'Mobilenum" WAS '02373643743' NOW '0983783738' CHANGED 4/11/2016 12.46pm
Do not want to try triggers...need php code....
We should not need to give any column names as it should run though them all dynamically...
Why do you not want to list the columns - Is this table going to change regularly?
You can still create the SQL dynamically - but store it as a static string.
Lots of examples for this (hint use information_schema.columns)
If the tables are not huge then I would p[rob write a bunch of Tests (1-per column) and UNION All them together - Then only return the Data where diff = true
Eg
Select * from
(
Select case when ISNULL(table1.columnA, -999) <> ISNULL(table2.columnA, -999) then 1 else 0 end as diff, table1.columna as Old_Val, table2.columnA as new_val
from table1 join table2 on ...
UNION ALL
/* etc etc * 150 ... */
) as x
where x.diff = 1
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
I'm very new with SQL and need assistance on how I can accomplish this task using the correct query.
I have 2 tables that I need to use. Table "TB1" has:
id Name
1 bob
2 blow
3 joe
table "TB2" has:
compid property
1 bob
2 blow
I am trying to get which compid is missing in "TB2" and insert it from "TB1"
the query I am doing is:
SELECT id, name from TB1, TB2 where id <> compid
what I get is 2 ouputs of Id 1, and 2, and 3 outputs from id 3. by using php:
for($i=0;$i <= mysql_num_rows($comp)-1; $i++)
{
echo mysql_result($comp, $i, 0)."<br>";
}
and I expected the ouput 3 but instead got this:
1
1
2
2
3
3
3
I understand its comparing all the rows within the table but is there a way to achieve what I am looking for?
Thanks for your time.
You are performing an implicit Cartesian JOIN which results in every row against every other row. You need to specify what attribute JOINs the two tables.
Using implicit syntax (not recommended):
SELECT id, name
FROM TB1, TB2
WHERE id <> compid
AND TB1.Name = TB2.property <-- Column join
Using explicit syntax:
SELECT id, name
FROM TB1
JOIN TB2
ON TB2.property = TB1.Name <-- Column join
WHERE id <> compid
To accomplish your goal you would need something along the lines of:
SELECT TB1.id, TB1.name
FROM TB1
LEFT JOIN TB2
ON TB2.property = TB1.Name
WHERE TB2.compid IS NULL
See it in action
It's best practice to always alias the columns you select to prevent ambiguity.
To select it you can do:
SELECT *
FROM TB1
WHERE id NOT IN (
SELECT compid
FROM TB2
);