sql MySQL error (1241) operand should contain 1 column(s) - mysql

first I will like to state that am still a newbie on writing SQL Queries. I thoroughly searched for an answer on this Error and I got a good number of answers, but none seems to be helpful or I will say I don't really know how to apply the solutions to mine.
Here is my challenge, I have an application table, that stores applicants records with some unique columns e.g (dl_number,parent_id,person_id). The parent_id keeps tracks of individual applicant history records with the his/her first record and each applicant is meant to have a unique dl_number, but for some reasons, some applicants dl_number(s) are not unique, hence a need to identify the records with changing dl_number(s).
Below is the SQL Query, that am getting the [sql error (1241) operand should contain 1 column(s)] error on.
SELECT id,application_id,dl_number,surname,firstname,othername,birth_date,status_id,expiry_date,person_id,COUNT(DISTINCT(dl_number,parent_id,birth_date)) AS NumOccurrences
FROM tbl_dl_application
WHERE status_id > 1
GROUP BY dl_number,parent_id,birth_date
HAVING NumOccurrences > 1
Please any help on how to solve this, or a better way to solve this.
Sample table and expected result

DISTICT is not really a function to be used that way.
You can do SELECT DISTICT column1, column2 FROM table to get unique rows only, or similarly SELECT column, count(DISTINCT anothercolumn) FROM table GROUP BY column to get unique rows within a group.
Problem as I understand it: You look for duplicates in your table. Duplicates are defined as having identical values of these 3 columns: dl_n‌​umber, parent_id and birth‌​_date.
I'm also assuming that id is a primary key in your table. If not, replace the t2.id <> t.id condition with one that uniquely identify your row.
If you only wanted to know what are the duplicated groups, this should work:
SELECT dl_n‌​umber, parent_id, birth‌​_date, count(*) as NumOccurences -- You can only add aggregation functions here, not another column unless you group by it.
FROM tbl_dl_application t
WHERE status_id > 1 -- I don't know what this is but it should do no harm.
GROUP BY dl_n‌​umber, parent_id, birth‌​_date
HAVING count(*)>1
If, however, you want to know details of each duplicated row, this query will give you that:
SELECT *
FROM tbl_dl_application t
WHERE
status_id > 1 -- I don't know what this is but it should do no harm.
AND EXISTS (
SELECT 1
FROM tbl_dl_application t2
WHERE
t2.dl_number = t.dl_number
AND t2.parent_id = t.parent_id
AND t2.birth_date = t.birth_date
AND t2.id <> t.id
)
ORDER BY dl_n‌​umber, parent_id, birth‌​_date, id; -- So you have your duplicates nicely next to each other.
Please explain further if I misunderstood your objective, or ask if the solution is not clear enough.

**You have to use only one column while use to DISTINCT function. You used this three field dl_number,parent_id,birth_date. Just use 1 filed from these 3. Then query will run.**
For example.
SELECT id,application_id,dl_number,surname,firstname,othername,birth_date,status_id,expiry_date,person_id,COUNT(DISTINCT(parent_id)) AS NumOccurrences
FROM tbl_dl_application
WHERE status_id > 1
GROUP BY dl_number,parent_id,birth_date
HAVING NumOccurrences > 1

Related

MySQL: Delete Duplicate entries from a row not working

I am trying to use the following code to remove duplicate entries from a column called herit. The name of my table is people
DELETE FROM people
WHERE herit NOT IN (SELECT *
FROM (SELECT MIN(n.herit)
FROM people n
GROUP BY n.herit) x)
Although this code is executed and it states "5 rows affected", it doesn't remove duplicates from my table in mysql.
What could possibly be wrong? I searched all over the internet, couldn't find anything satisfactory
Try this one..
DELETE FROM people WHERE id NOT IN
(SELECT id FROM (SELECT id FROM people GROUP BY herit HAVING count(herit) > 1
OR count(herit) = 1) newTable)
This is actual and resultant table

How to make this one to many query?

I need help makeing this selection.
Those are the 2 tables.
First table:
-there are Unique/Primary buyID-s( we can say that here buyID is and Order)
-distID-s can be many.
Second table:
-This table does not have an unique indentifier
-same buyID here ca more than one(basically buyID here offers more details on order, offers the a list of products for that order(buyID) from first table.
What I am trying to do, but I can't figure how to write the query:
-Select all buyID-s Where distID=119 From table 1(buyID-s will be 1724,1833 and 1890)
-From Table 2, sum the quantity, where buyID(from table2)=buyID(from table1).
As a result I must have:
distID=119
buyID=1724--------quantity=25 (as an example)
buyID=1833--------quantity=60
buyID=1890--------quantity=23(if there will also be the product_number list, will be awesome)
I hope this make sense.
I am not too much experienced,I am trying for # an hour, I am sure is not too hard, but this piss me of...
I am waiting for abit of help.
Thanks
Use LEFTJOIN and GROUP BY
select t1.distID, t2.buyID,sum(t2.quantity) as quantity
from table1 t1
left join table2 t2 on t2.buyID = t1.buyID
where t1.distID = 119
group by t1.distID, t2.buyID
If you also want to include product_number in resultset, then you have to select them using aggregate function like MAX(product_number) as product_number since the query use GROUP BY clause.
select sum(quantity) from Table2 where buyID in (select buyID from Table1 where distID=119)

MYSQL - QUERY FROM TWO TABLES

Question - let's say I have 2 tables.
Table 1 - name is permission_list, columns are ID (unique ID), col_ID, user_ID
Table 2 - name is list_entries, Columns are ID (unique ID), title, description, status
I want to select all the rows from table 2 that have status of 'public' as well as all the rows from table 2 that the ID from table 2 shows up in table 1 (under the column col_ID) AND if the user_ID in table 1 matches a certain value. So, anything public, or anything that this specific user has listed under the permissions table. This query would also remove duplicates - in case the user gets a public entry listed in their permissions_list, it wouldn't show up twice.
Hope that makes sense!
Here you go:
SELECT DISTINCT table2.* from table2
LEFT JOIN table1 USING (id)
WHERE status='public'
OR user_ID='someuser';
You need to get some education on JOIN for your first thing, and the second thing is called DISTINCT.
Start here... https://www.google.com/
You have not specified your join condition so we can't give you code samples really. Also the way you worded your question, I'm not entirely sure you don't want a UNION. Read up on those concepts and come back here when you can improve the question.
SELECT table_2.status, table_2.ID, table_1.col_ID
FROM table_1 JOIN table_2
WHERE table_2.status = 'public'
AND table_2.ID = table_1.col_ID
AND table_1.user_ID = 'certain value'
;
Try this

sql delete all but 2 duplicates

I want to be able to limit the amount of duplicate records in a mySQL database table to 2.
(Excluding the id field which is auto increment)
My table is set up like
id city item
---------------------
1 Miami 4
2 Detroit 5
3 Miami 4
4 Miami 18
5 Miami 4
So in that table, only row 5 would be deleted.
How can I do this?
MySQL has some foibles when reading and writing to the same table. So I don't actually know if this will work, the syntax is fine in many implementations of SQL, but I don't know if it's MySQL friendly...
DELETE
yourTable
WHERE
1 < (SELECT COUNT(*)
FROM yourTable as Lookup
WHERE city = yourTable.city AND item = yourTable.item AND id < yourTable.id)
EDIT
Amazingly convoluted, but worth a try?
DELETE
yourTable
FROM
yourTable
INNER JOIN
(
SELECT
id
FROM
(
SELECT
id
FROM
yourTable
WHERE
1 < (SELECT COUNT(*)
FROM yourTable as Lookup
WHERE city = yourTable.city AND item = yourTable.item AND id < yourTable.id)
)
AS inner_deletes
)
AS deletes
ON deletes.id = yourTable.id
I think your problem here is that both your code and/or table structure allows inserting duplicates and you are asking this question when you should really fix your db and/or code.
i think a better solution is avoid allow more than 5 registers, you have to implement a validation where if select count(*) > 3 you will not accept the new insert.
because if you want to do this into the data tier, you have to use a stored procedure , because first you need to identify all the register with more than 3 registers and delete only the last .
Saludos
Due to MySQL being notoriously difficult when it comes to updating queried tables (see for example the answers from Dems), the best I can figure out is sadly more than one statement but on the plus side fairly readable;
CREATE TEMPORARY TABLE Dump AS SELECT id FROM table1 WHERE id NOT IN
(SELECT MIN(id) FROM table1 GROUP BY city,item UNION
SELECT MAX(id) FROM table1 GROUP BY city,item);
DELETE FROM table1 where id in (select * from Dump);
DROP TABLE DUMP;
Not sure if it was important which duplicate was removed, this keeps the first and last.
In your reply to Joachim's answer, you ask about saving 3 or 5 rows, this is one way to accomplish it. Depending on how you are using this database, you could either call this in a loop, or you could turn it into a stored procedure. Either way, you would continue to run this entire block of code until Rows Affected = 0:
drop table if exists TempTable;
create table TempTable
select city, item,
count(*) as record_count,
min(id) as ItemToDrop -- this could be changed to max() if you
-- want to delete new stuff instead
from YourTable
group by city, item
having count(*) > 2; -- This value = number of rows you save
delete from YourTable
where id in (select ItemToDrop from TempTable);

Deleting duplicate rows with sql

I am trying to delete duplicate rows from my mysql table. I've tried multiple queries but I am keep on getting this error: #1093 - You can't specify target table 'usa_city' for update in FROM clause
The table looks like this:
usa_city
--------
id(pk)
id_state
city_name
And the queries I have tired were:
DELETE FROM usa_city
WHERE id NOT IN
(
SELECT MIN(id)
FROM usa_city
GROUP BY city_name, id_state
)
And:
DELETE
FROM usa_city
WHERE usa_city.id IN
-- List 1 - all rows that have duplicates
(SELECT F.id
FROM usa_city AS F
WHERE Exists (SELECT city_name, id_state, Count(id)
FROM usa_city
WHERE usa_city.city_name = F.city_name
AND usa_city.id_state = F.id_state
GROUP BY usa_city.city_name, usa_city.id_state
HAVING Count(usa_city.id) > 1))
AND usa_city.id NOT IN
-- List 2 - one row from each set of duplicate
(SELECT Min(id)
FROM usa_city AS F
WHERE Exists (SELECT city_name, id_state, Count(id)
FROM usa_city
WHERE usa_city.city_name = F.city_name
AND usa_city.id_state = F.id_state
GROUP BY usa_city.city_name, usa_city.id_state
HAVING Count(usa_city.id) > 1)
GROUP BY city_name, id_state);
Thanks in advance.
Try to select the duplicates first, the delete them
DELETE FROM usa_city WHERE city_id IN
(
SELECT city_id FROM usa_city
GROUP BY city_name, id_state
HAVING count(city_id) > 1
)
Hope it helps!!!
MODIFIED: Based on the comment, if you want to keep one record, you can make a join and keep the lowest value
DELETE c1 FROM usa_city c1, usa_city c2 WHERE c1.id < c2.id AND
(c1.city_name= c2.city_name AND c1.id_state = c2.id_state)
Be sure to make a backup before executing the query above...
from mysql documentation:
"Currently, you cannot delete from a table and select from the same
table in a subquery."
but here is a workaround for update, should work for delete too.
also, you could select rows, and then in php for example delete them in loop
You may found here an answer to your problem: How to delete duplicate records in mysql database?
You should improve your database by using keyfields to prevent duplicate rows, so you dont need to clear in future.
Edit : This solution is also found if you follow the link posted by BloodyWorld, so if it works please go and upvote DMin's post here
Found this browsing the internet (#1 google result for mysql delete duplicate rows), have you tried it?
delete from table1
USING table1, table1 as vtable
WHERE (NOT table1.ID=vtable.ID)
AND (table1.field_name=vtable.field_name)
Judging from your examples, when you say "duplicate", you mean "having the same combination of id_state and city_name", correct? If so after you have done removing the duplictes, I strongly suggest creating a UNIQUE constraint on {id_state, city_name}.
To actually remove the duplicates, it is not enough to just identify the set of duplicates, you must also decide which of the identified duplicates to keep. Assuming you want to keep the ones with the smallest id, the following piece of SQL will do the job:
CREATE TEMPORARY TABLE usa_city_to_delete AS
SELECT id FROM usa_city T1
WHERE EXISTS (
SELECT * FROM usa_city T2
WHERE
T1.id_state = T2.id_state
AND T1.city_name = T2.city_name
AND T1.id > T2.id
);
DELETE FROM usa_city
WHERE id IN (SELECT id FROM usa_city_to_delete);
DROP TEMPORARY TABLE usa_city_to_delete;
Unfortunately, MySQL does not allow the correlated subqueries in DELETE, otherwise we could have done that in a single statement, without the temporary table.
--- EDIT ---
You can't have a correlated subquery but you can have JOIN, as illustrated by Carlos Quijano answer. Also, the temporary table can be created implicitly, as suggested by Kokers.
So it is possible to do it in a single statement, contrary to what I wrote above...