MySql how to use UPDATE with HAVING? - mysql

My table1 looks like:
id name_co name_r temp sld
1 name1 1 ... ...
2 name2 1 ... ...
3 name2 1 ... ...
4 name2 1 ... ...
5 name3 1 ... ...
6 name2 1 ... ...
I need to increment name_r if there are two or more identical name_co.
To be so:
id name_co name_r temp sld
1 name1 1 ... ...
2 name2 1 ... ...
3 name2 2 ... ...
4 name2 3 ... ...
5 name3 1 ... ...
6 name2 4 ... ...
I tried different options and I came to this:
UPDATE table1
SET name_r = name_r + 1
WHERE (SELECT COUNT(*)
GROUP BY name_co
HAVING name_co > 1)
The query works and returns 0 rows, but I know that in some way he's wrong, but I can't figure out what. Can anyone help? (And a bit of explanation, so I better understood)

--updated intended targets
UPDATE table1
SET name_r = name_r + 1
WHERE id IN
(
-- return those ids again (to avoid the mysql #1093 error)
SELECT id
FROM
( -- get all the ids for those names
SELECT id
FROM table1
WHERE name_co IN
( -- get all names that have more than one id
SELECT name_co
FROM table1
GROUP BY name_co
HAVING COUNT(id) > 1
)
) a
)

Related

MySQL count two rows as one

I need to count matches in a database.
Input:
id_to id_from
1 2
2 1
1 3
3 1
1 4
5 1
the 5th and 6th row has only one direction so doesn't count
Sample Output:
id_match
1
2
3
So, for 1 (implicit), 2 and 3 there is a reverse match but for 4 and 5 there aren't.
---- EDITED ----
Supposing the table name is "example" and I want to get all matches of id=1 then the SQL query will be:
SELECT count(*) FROM
(SELECT id_to FROM example WHERE id_from = 1) as t1,
(SELECT id_from FROM example WHERE id_to = 1) as t2
WHERE t1.id_to = t2.id_from
but maybe there is a better way to do it
You could try
SELECT DISTINCT id_from AS matched_id
FROM your_table AS data1
WHERE EXISTS (SELECT 1
FROM your_table AS data2
WHERE data1.id_from = data2.id_to
AND data1.id_to = data2.id_from)
I've created a demo here

Mysql statement, if at least one row contains result

i've a table with records:
----------------------------------
ID | UniqueId | Name | Result
----------------------------------
1 1 Test1 OK
2 1 Test1 Cancelled
3 1 Test1 OK
4 2 Test2 OK
5 2 Test2 OK
6 2 Test2 OK
7 2 Test2 OK
8 3 Test3 OK
9 3 Test3 OK
Let's say i wan't to check if at least one row with UniqueId = 1 not contains Result == Cancelled. To exclude record with UniqueId = 1, because it is cancelled.
How can i do this?
Thank you
SELECT t1.* from table as t1 where t1.UniqueId not in(select t.UniqueId from table as t
where t.Result="Cancelled")
Just ask for rows with UniqueId = 1 and Result != Cancelled
SELECT ID FROM table WHERE UniqueId = 1 AND Result <> 'Cancelled' LIMIT 1
I'd go this way, but the other answers are a bit easier in terms of syntax.
SELECT UniqueId, Name
FROM Table T
GROUP BY UniqueId, Name
HAVING Result != 'Cancelled'
SELECT id FROM mytable WHERE uniqueId = 1 AND result != 'Cancelled'

MYSQL add row with id=first bank AND that does not exist on another table

With this 2 tables:
table1
----
id(ai) id_aaa
1 1
31 2
32 3
43 5
46 8
table2
----
id record
1 4
I need a select that gives me table1 id_aaa=6. Next available id aaa that does not exist as record in table2.
This should do the trick:
SELECT MIN(id_aaa + 1) AS next_one FROM table1 t1
LEFT JOIN table1 t1bis ON t1.id_aaa + 1 = t1bis.id_aaa
WHERE t1bis.id_aaa IS NULL AND id_aaa + 1 NOT IN (SELECT record FROM table2);

Find duplicate records in MySQL without named column

I have a table like this:
**lead_id** **form_id** **field_number** **value**
1 2 1 Richard
1 2 2 Garriot
2 2 1 Hellen
2 2 2 Garriot
3 2 1 Richard
3 2 2 Douglas
4 2 1 Tomas
4 2 2 Anderson
Where field_number = 1 is the name and field_number = 2 is the surname.
I would like to find entries that are equal by name OR surname and group them by lead_id, so the output could be like this:
1
2
3
Any thoughts on how this can be done?
This should work and be reasonably efficient (depending upon indexes):
select distinct lead_id
from tablename as t1
where exists (
select 1
from tablename as t2
where t1.field_number = t2.field_number
and t1.value = t2.value
and t1.lead_id <> t2.lead_id
)
Select leadid from (
Select DISTINCT leadid,value from tablename
Where fieldnumber=1
Group by leadid,value
Having count(value) >1
Union all
Select DISTINCT leadid,value from tablename
Where fieldnumber=2
Group by leadid,value
Having count(value) >1
) as temp
Surely there is a faster option

mysql, find frequenties in table

I have a table in PHPMyAdmin with six columns.
In each cell there is a name.
Now I want to know in what frequencies we have each name in each column.
For example:
column1 column2 column3
name1 name3 name2
name1 name2 name2
name2 name3 name1
Then I need a list with:
column1 column2 column3
name1 - 2 0 1
name2 - 1 1 2
name3 - 0 2 0
I tried to play with:
SELECT Count(*) FROM aanmeldingen2013 WHERE column1 LIKE name1.
Can someone help me with the SQL code to generate this output?
I think this is the most efficient method:
select name,
sum(n = 1) as Column1Cnt,
sum(n = 2) as Column1Cnt,
sum(n = 3) as Column1Cnt
from (select (case when n.n = 1 then column1
when n.n = 2 then column2
when n.n = 3 then column3
end) as name,
n.n
from t cross join
(select 1 as n union all select 2 union all select 3) n
) t
This should be more efficient that a union all query because it only scans the original table once. I've shown the example here for three columns (as in your sample data). It should be clear how to generalize this to six columns.
check this query
SELECT COUNT( * ) FROM 'tbl_name' where column1='name1';