select count(distinct (mobile)) from number_data where value = 'A_HNI';
count(distinct (mobile)) = 5046082
select count(mobile) from number_data where value = 'A_HNI';
count(mobile) = 9658150
There are 4612068 duplicates of mobile numbers in value = 'A_HNI'. I want to delete the duplicates and keep the original . The table has no primary key nor indexing . I can't assign a primary key but indexing is possible . The table is as follows :
Field Type Null Key Default Extra
title | varchar(255) | YES | | NULL | |
name | varchar(255) | YES | | NULL | |
age | varchar(255) | YES | | NULL | |
pincode | varchar(255) | YES | | NULL | |
city | varchar(255) | YES | | NULL | |
state | varchar(255) | YES | | NULL | |
mobile | varchar(255) | YES | | NULL | |
source | varchar(255) | YES | | NULL | |
value | varchar(255) | YES | | NULL | |
dnd | varchar(255) | YES | | NULL | |
msc | varchar(255) | YES | | NULL | |
operator | varchar(255) | YES | | NULL | |
Get the Distinct value from your table ,create a new table with it then delete the old table
For reference you can check this page
Remove Duplicate Value
You can also refer this stack answerenter link description here
Related
I created a table in mysql. Every column in my table I created with NOT NULL. What does the NULL value in my DEFAULT column mean? I never plan to have a NULL value in those columns.
+------------+--------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+-------------------+-------+
| user_id | binary(16) | NO | PRI | | |
| first_name | varchar(255) | NO | | NULL | |
| last_name | varchar(255) | NO | | NULL | |
| email | varchar(255) | NO | UNI | NULL | |
| digest | binary(40) | NO | | NULL | |
| join_ts | timestamp | NO | | CURRENT_TIMESTAMP | |
+------------+--------------+------+-----+-------------------+-------+
This means you don't have a default value for these columns.
If you have a default value, you can create an insert query without that column.
With your table, you must give a value to all the null default's columns
Initially the Primary Key was
(caseId,osName)
+--------------------+---------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+---------------+------+-----+-------------------+-------+
| createdBy | varchar(200) | NO | | NULL | |
| createdOn | timestamp | NO | | CURRENT_TIMESTAMP | |
| osName | varchar(200) | NO | | NULL | |
| caseId | varchar(100) | NO | MUL | NULL | |
| issueType | varchar(100) | NO | | NULL | |
| observationState | varchar(60) | YES | | NULL | |
| problemSolving | int(11) | NO | | NULL | |
| structure | int(11) | NO | | NULL | |
| logicalTransition | int(11) | NO | | NULL | |
| ownership | int(11) | NO | | NULL | |
| expectationSetting | int(11) | NO | | NULL | |
| empathy | int(11) | NO | | NULL | |
| valueAdd | int(11) | NO | | NULL | |
| comments | varchar(2000) | NO | | NULL | |
| version | int(11) | YES | | 0 | |
| showcase_escalate | varchar(30) | YES | | NULL | |
| ringback_state | varchar(200) | YES | | NULL | |
| trt_state | varchar(200) | YES | | NULL | |
| srp_state | varchar(200) | YES | | NULL | |
| id | int(11) | NO | PRI | NULL | |
+--------------------+---------------+------+-----+-------------------+-------+
In the above schema, id was AUTO INCREMENT. I removed that so I could make caseId as PRIMARY KEY. But since there are multiple records with same caseId I am not able to make the change.
At the end I just want caseId as my Primary Key.
Initially the Primary Key was (caseId,osName).
As defined in the MySQL Glossary:
primary key
A set of columns -- and by implication, the index based on this set of columns -- that can uniquely identify every row in a table. As such, it must be a unique index that does not contain any NULL values.
InnoDB requires that every table has such an index (also called the clustered index or cluster index), and organizes the table storage based on the column values of the primary key.
When choosing primary key values, consider using arbitrary values (a synthetic key) rather than relying on values derived from some other source (a natural key).
See Also clustered index, index, natural key, synthetic key.
Since you have multiple records with the same value for caseId, caseId alone cannot "uniquely identify every row" in your table; and therefore caseId alone cannot be a PRIMARY KEY.
mysql> desc customer_delivery_loc;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| cust_loc_id | int(11) | NO | PRI | NULL | auto_increment |
| customer_id | int(11) | YES | MUL | NULL | |
| locality | varchar(100) | YES | | NULL | |
| area | varchar(100) | YES | | NULL | |
| address | varchar(100) | YES | | NULL | |
| city | varchar(50) | YES | | NULL | |
| state | varchar(50) | YES | | NULL | |
| phone_number | bigint(20) | YES | | NULL | |
| +---------------+--------------+------+-----+---------+----------------+
mysql> desc vendor_home_delivery;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| vendor_id | varchar(50) | YES | MUL | NULL | |
| locality | varchar(100) | YES | | NULL | |
| area | varchar(100) | YES | | NULL | |
| address | varchar(100) | YES | | NULL | |
| city | varchar(50) | YES | | NULL | |
| state | varchar(50) | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+
I have got two Tables as shown above
The area column is common for both the tables .
From User Interface area and phone_number will be passed .
How to write a query to display the data from both the tables based on phone_number and area ??
I tried this way but i am not sure and its giving error
select c.phone_number
from customer_delivery_loc c
where c.area in (select locality , address , area from vendor_home_delivery )
where c.phone_number = '9848032919';
- MySQL Database Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where c.phone_number = '9848032919'' at line 1
You need
and c.phone_number
in place of
where c.phone_number
in your query.
As one mistake pointed by already by #Ollie Jones for another error Operand should contain 1 column(s) error you can rewrite your query by using join
select c.phone_number
from customer_delivery_loc c
join vendor_home_delivery v
on(c.area = v.locality or c.area=v.address or c.area=v.area)
where c.phone_number = '9848032919';
Cause of this error is because you are comparing single column area with 3 columns using in()
I have three tables, emails, person_details and data_providers. Basically all of my users id, email, and current assigned data_providers_id are stored in the emails table.
The second table, person_details contains demographic information collected by multiple data providers, each row identified by an emails_id that is relational to the emails.id data_providers_id that is relational to the third table data_providers.id
The third table, data_providers contains each of my data providers id, name, and precedence.
Basically, a users information could be collected from multiple sources, and I need to UPDATE emails set data_providers_id = based on a select that would JOIN the person_details table and the data_providers table sorting by data_providers.precedence DESC then person_details.import_date ASC and use the first value (highest precedence, then oldest import_date).
I was trying to build the query, but my subquery is returning more than one row. This query is a little over my head, hoping someone more experienced with complex queries might be able to point me in the right direction.
UPDATE emails
SET emails.data_providers_id =
SELECT person_details.data_providers_id
FROM person_details
LEFT JOIN data_providers ON person_details.data_providers_id = data_providers.id
ORDER BY data_providers.percent_payout ASC, person_details.import_date ASC ;
Here are some details about the three tables if this helps. Any guidance would be MUCH appreciated. Thanks in advance :)
emails table:
+-------------------+---------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+---------------------+------+-----+---------------------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| data_providers_id | tinyint(3) unsigned | NO | MUL | NULL | |
| email | varchar(255) | NO | UNI | NULL | |
+-------------------+---------------------+------+-----+---------------------+----------------+
person_details:
+-------------------+---------------------+------+-----+---------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+---------------------+------+-----+---------------------+-------+
| emails_id | int(11) unsigned | NO | PRI | NULL | |
| data_providers_id | tinyint(3) unsigned | NO | PRI | NULL | |
| fname | varchar(255) | YES | | NULL | |
| lname | varchar(255) | YES | | NULL | |
| address_line1 | text | YES | | NULL | |
| address_line2 | text | YES | | NULL | |
| city | varchar(255) | YES | | NULL | |
| state | varchar(2) | YES | | NULL | |
| zip5 | varchar(5) | YES | | NULL | |
| zip4 | varchar(4) | YES | | NULL | |
| home_phone | varchar(10) | YES | | NULL | |
| mobile_phone | varchar(10) | YES | | NULL | |
| work_phone | varchar(10) | YES | | NULL | |
| dob | date | YES | | NULL | |
| gender | varchar(1) | YES | | NULL | |
| ip_address | varchar(15) | NO | | NULL | |
| source | varchar(255) | NO | | NULL | |
| optin_datetime | datetime | NO | MUL | NULL | |
| import_date | timestamp | NO | | 0000-00-00 00:00:00 | |
+-------------------+---------------------+------+-----+---------------------+-------+
data_providers table:
+-----------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+---------------------+------+-----+---------+----------------+
| id | tinyint(3) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| precedence | int(2) | YES | | 0 | |
+-----------------+---------------------+------+-----+---------+----------------+
To use a SELECT as an expression you have to put it in parentheses. And to get the first value, use LIMIT 1:
UPDATE emails
SET emails.data_providers_id = (
SELECT person_details.data_providers_id
FROM person_details
LEFT JOIN data_providers ON person_details.data_providers_id = data_providers.id
WHERE person_details.emails_id = emails.id
ORDER BY data_providers.percent_payout ASC, person_details.import_date ASC
LIMIT 1) ;
I want to do something along the lines of this:
delete c.* from Club c
where c.id in(select club_id from ClubAttrValues where value='E5');
but when I try this I get the error
Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`foroige`.`clubattrvalues`, CONSTRAINT `FKAE90CBE9EE3B7895` FOREIGN KEY (`club_id`) REFERENCES `Club` (`id`))
Is there any way to delete everything to do with the every club in Club where the value in ClubAttrValues is 'E5'. There are multiple rows in ClubAttrValues with the same club_id, which would all ideally be deleted, and club_id references id in Club.
I have tried setting ON DELETE CASCADE in MySQL workbench but even this seems to make no difference.
The Club table looks like this:
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| created | datetime | YES | | NULL | |
| deleted | tinyint(1) | NO | | NULL | |
| modified | datetime | YES | | NULL | |
| address1 | varchar(255) | NO | | NULL | |
| address2 | varchar(255) | YES | | NULL | |
| country | varchar(255) | YES | | NULL | |
| county | varchar(255) | NO | | NULL | |
| email | varchar(255) | YES | | NULL | |
| fax | varchar(255) | YES | | NULL | |
| mobile | varchar(255) | YES | | NULL | |
| name | varchar(255) | NO | | NULL | |
| postcode | varchar(255) | YES | | NULL | |
| telephone | varchar(255) | YES | | NULL | |
| town | varchar(255) | YES | | NULL | |
| type | varchar(255) | NO | | NULL | |
| parent_id | bigint(20) | YES | MUL | NULL | |
| lft | bigint(20) | NO | | NULL | |
| rgt | bigint(20) | NO | | NULL | |
+-----------+--------------+------+-----+---------+----------------+
and my ClubAttrValues table:
+---------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| value | text | YES | | NULL | |
| club_id | bigint(20) | YES | MUL | NULL | |
+---------+--------------+------+-----+---------+----------------+
Before deleting try:
SET FOREIGN_KEY_CHECKS=0
Set it against to 1 when you are done.
It works when you set on delete cascade for the foreign key column and then
delete c
from Club c
inner join ClubAttrValues a on a.club_id = c.id
where a.value = 'E5'
See this SQLFiddle demo