Can you help me with mysql query ? I would like to empty all duplicated product name and keep ONLY one row (product name and its SKU).
I wrote the query like this. Can you please confirm If it's the right syntax
UPDATE tvcom_product t1 JOIN
(SELECT name
FROM tvcom_product
GROUP BY name
HAVING count(product_id) > 1) dup ON t1.name = dup.name
SET t1.name = '' WHERE t1.sku != ''
UPDATE tvcom_product t1
JOIN tvcom_product t2 ON t2.name = t1.name AND t2.sku < t1.sku
SET t1.name = ''
This will reset name for all the records which have duplicates with same name. It will leave just one record with the smallest (alphabetically) SKU.
Instead of t2.sku < t1.sku you can write t2.id < t1.id - should be more optimal, it will leave out the record with smallest ID.
Upd. This is simplest but not most optimal way. For huge tables this one should work better:
UPDATE tvcom_product t1
JOIN (
SELECT name, MIN(id) id
FROM tvcom_product
GROUP BY name
HAVING COUNT(*) > 1
) t2 ON t1.name = t2.name AND t1.id != t2.id
SET t1.name = ''
If you want to optimise it even more then you should store the sub-query in a separate table and join it. Of course id in this query can be changed with sku.
Related
I have this MySQL query which links two tables.I want to join theme where to get not existed data from the other table.
this here get the registered date for users
SELECT
t2.name ,phone
FROM
(SELECT name,tid,date_d,class_time AS 'absent'
FROM absent where date_d = '2016-12-04' ) t1
JOIN
(SELECT name, id,phone AS 'phone'
FROM users ) t2
ON t1.tid = t2.id
group by id
I want the users who are not registered in table t1 from table two
I used the same above query but with something like this ON t1.tid != t2.idit works only with unduplicated date
SELECT
t2.name ,phone
FROM
(SELECT name,tid,date_d,class_time AS 'absent'
FROM absent where date_d = '2016-12-04' ) t1
JOIN
(SELECT name, id,phone AS 'phone'
FROM users ) t2
ON t1.tid != t2.id
group by id
A condition like ON t1.tid != t2.id used in a join will very likely give you a result if either t1 or t2 contain more than one row (because then, for some of the rows in t1 and t2, tid and id will be different). What you are looking for is "those users that have NOT been absent at a specific day, i.e. where no entry in absent-table exists", right?
Try the following:
SELECT name,id,phone AS 'phone'
FROM users t2
WHERE t2.id not in
(SELECT tid
FROM absent where date_d = '2016-12-04')
Here's my questions in writing some scripts in MySQL:
I get a table T1 with some columns called id, t1_col_01, t1_col_02, and a table T2 with some columns called id, t2_col_01, t2_col_02.
For each row R1 in T1, I want to update R1.t1_col_01 = 'Yes' if the there are multiple rows in T2 that has the same id column with R1.id. If not, set R1.t1_col_01 = 'No'.
I tried to write:
update T1, T2
set
T1.t1_col_01 = 'Yes'
where
(select count(*) from T2 where T2.id = T1.id) > 1
But it didn't work.
What you need is this:
update T1
inner join T2
on ( T2.id = T1.id )
set T1.t1_col_01 = 'Yes'
where (select count(*) from T2 where T2.id = T1.id) > 1
See it here on fiddle:
http://sqlfiddle.com/#!2/0edc4/1
I have been using the following SQL:
SELECT DISTINCT NAME
FROM Events t1
LEFT JOIN UserHistory t2 ON t1.Name = t2.Event
WHERE t2.Event IS NULL
To select all rows from table 1 where table 2 is Null. This effectively filters out all my Table 1 data where Table 2 has data. However, I want to apply this only when a column in table 2 equals a certain value. Therefore I am looking to do a SELECT * FROM t2 WHERE t2.ID = 1 but am unsure how this fits into this query.
SELECT DISTINCT NAME
FROM Events t1
LEFT JOIN UserHistory t2 ON t1.Name = t2.Event and t2.certain_column = 1234
WHERE t2.Event IS NULL
Also you can try query with NOT EXISTS:
SELECT DISTINCT NAME
FROM Events t1
WHERE NOT EXISTS(SELECT * FROM UserHistory t2
WHERE t1.Name = t2.Event AND t2.ID = 1)
You need to add the predicate to the JOIN condition:
SELECT DISTINCT NAME
FROM Events t1
LEFT JOIN UserHistory t2 ON t1.Name = t2.Event AND t2.ID = 1
WHERE t2.Event IS NULL;
If you add it to the WHERE you effectively turn your outer join into an inner join, meaning no rows will be returned (since NULL = 1 evaluates to false)
Ok. I have some data in one table, that references on multiple occasions some data in another table.
Table1 - main client table
Table2 - user defined fields
Say I have a query that shows a client id from Table1 and all attached / used "used defined fields" from Table2
SELECT t1.Id, t2.udf
FROM Table1 t1
JOIN Table2 t2 ON t1.Id = t2.Index
WHERE t1.EndDate IS NULL AND
t1.Id = '1234.9876' AND
I would get the following for a result...
ID UDF
1234.9876 100
1234.9876 110
1234.9876 118
1234.9876 124
1234.9876 198
1234.9876 256
Now, say I wanted to query this same thing, and get ONLY the ID of the Client, but ONLY IF a value for t2.udf equaling '194' did not exist. So, I would simply get
ID
1234.9876
...as a result.
Make the join a LEFT join and filer where t2.Index is null
SELECT t1.Id
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.Id = t2.Index
AND t2.UDF = 194 -- has to be before where clause
WHERE t2.Index IS NULL
AND t1.EndDate IS NULL
AND t1.Id = '1234.9876' -- not sure if you want this part
Another way by using NOT EXISTS
SELECT t1.Id
FROM Table1 t1
WHERE NOT EXISTS (SELECT 1 FROM Table2 t2 WHERE t1.Id = t2.INDEX
AND t2.UDF = 194)
AND t1.EndDate IS NULL
AND t1.Id = '1234.9876'
See also JOINS
You can add AND t2.udf not in (select udf from table2 where udf <> '194').
But #SQLMenace solution is better
This should do it.
SELECT DISTINCT t1.Id
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.Id = t2.Index
WHERE t2.UDF NOT IN (194)
AND t2.Index IS NULL
Select DISTINCT gives you unique entries that satisfy the other conditions, and the first where clause
t2.UDF NOT IN (194)
Normall would return all the rows for the t1 where the t2.UDF is not 194, but it is limited by the Select Distinct to give you only distinct id's
Try the following:
SELECT t1.Id
FROM Table1 t1
JOIN Table2 t2 ON t1.Id = t2.Index
WHERE t1.EndDate IS NULL AND
t1.Id = '1234.9876' AND
t2.udf <> '194'
I'm trying to update several rows with email addresses. T1.email is to be updated with T2.email based on T1.name existing in T2.name.
Currently, the query looks like this.
UPDATE T1
SET T1.email = T2.email
FROM Table1 T1
INNER JOIN Table2 T2 ON T1.name = T2.name
WHERE T1.name = T2.name
AND (Some conditions)
LIMIT 1398
A similiar question is asked here, but a syntax error is given.
SQL UPDATE SET one column to be equal to a value in a related table referenced by a different column?
I've also tried updating with ANY.
UPDATE Table1
SET Table1.email = ANY
(
SELECT Table2.email FROM Table2
WHERE Table1.accountid = 901234
AND Table2.pid = 123
AND Table2.email IS NOT NULL
)
WHERE Table1.name IN
(
SELECT Table2.name FROM Table2
WHERE Table1.accountid = 19574
AND Table2.pid = 123
AND Table2.email IS NOT NULL
)
LIMIT 1398
Currently, this returns an error "SQL Error (1242): Subquery returns more than 1 row".
May be the beginning of a copy and paste job!
As detailed under UPDATE Syntax, for MySQL you want:
UPDATE Table1 T1 JOIN Table2 T2 USING (name)
SET T1.email = T2.email
WHERE (Some conditions)
LIMIT 1398