Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I have MySQL database and a have a table named CALLER. In table caller I need to check if in column USERNAME there are values which exist more than once and if exist to list all these values.
Thank you
Ususlly you can do this using GROUP BY and HAVING COUNT(*)>1
SELECT USERNAME FROM CALLER
GROUP BY USERNAME
HAVING COUNT(*)>1
Update: To get all duplicate rows:
SELECT * FROM CALLER WHERE USERNAME IN
(
SELECT USERNAME FROM CALLER
GROUP BY USERNAME
HAVING COUNT(*)>1
)
Before getting to the answer....
It would have been a help if you'd provided the full table structure (as a CREATE TABLE statement)
If you need to apply this exercise it implies your database design is wrong - and after you've identified the duplicates and resolved them then you should add a unique index on the relevant column.
Assuming that you've got an auto-increment field, or some other value (such as created date) which differentiates between rows with the same USERNAME....
SELECT a.id, username, b.id
FROM caller a
INNER JOIN caller b
ON a.username=b.username
AND b.id>a.id
Note that this will report some rows more than once if the username exists for more than 2 rows. Alternately:
SELECT username, COUNT(DISTINCT id), MIN(id), MAX(id)
FROM caller
GROUP BY username
HAVING count(*)>1
But this won't explicitly identify all the ids where there are more than 2 rows with a specific username.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I've been having a lot of trouble with a Join statement that I can't get to work. I'm relatively new to MySQL so would appreciate some help.
I have 2 tables under the database "CoreProtect", co_command and co_user. co_command has 2 columns, message and user. co_user has another 2 columns, "rowid" and "user". rowid refers to the "user" column in co_command, where the "user" from co_command is the same as the "row_id" in co_user, the user field in co_user stores the actual string name of the user. (I hope that makes sense)
The query I am using is:
SELECT user
COUNT(message)
FROM co_command
GROUP BY user
HAVING COUNT(message)
ORDER BY count(message)
This does what I expect, however it gives the numerical value that refers to the other table of the names. I'd like the output to give the Name of the User and the count, instead of the numerical refrence to the other table.
I know I need a JOIN statement for this and I've researched it but have been unable to make it work.
I can find the string username from the other table with
SELECT user
FROM co_user
WHERE rowid="User Value from co_command";
Thanks for any help.
seems you need a join
SELECT c.user, u.user
, COUNT(message)
FROM co_command c
INNER JOIN co_user u ON. u.rowid = c.user
GROUP BY c.user, u.user
ORDER BY count(message)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have 2 different tables. Both have several different columns, but there's a column serial_number that is in both these tables with duplicate serial numbers.
In the table 1, I want to append the row data of table 2 if the serial_number matches in both the tables.
Example:
Table 1
id, serial_number, name , phone , email
1, 87454126 , Chris , 5105487451, example#example.com
Table 2
id, serial_number, status , reason
1, 87454126 , Completed, Some Reason
The result I want:
Table 1 changes to:
1, 87454126, Chris, 5105487451, example#example.com, Completed, Some Reason
Kindly help me, I can't figure this out. Thanks
You can give both tables a different name and join like:
SELECT *
FROM Table1 AS t1
JOIN Table2 AS t2 ON t1.serial_number = t2.serial_number;
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm trying to select data from one table based on a date range of data from another table.
First table: clients with fields firstname, lastname and telephone.
Second table: orders with a field date
I need to get a list of all clients first name, last name and telephone numbers who have orders placed between 2019-12-1 and 2019-12-16.
I've tried many iterations of this mysql select but get way too many hits.
SELECT clients.firstname, clients.lastname, clients.telephone
from clients
JOIN orders ON date BETWEEN '2019-12-1 00:00:00' AND '2019-12-16 23:59:59'
Any insight would be appreciated.
I would assume that the orders has a foreign key column that references the client each order belongs to, and that this column is called client_id. Without this, your schema would not make sense.
I would solve this with exists and a correlated subquery that ensures that the given client placed at least one order during the concerned date range.
select
c.firstname,
c.lastname,
c.telephone
from clients c
where exists (
select 1
from orders o
where
o.client_id = c.client_id
and o.date >= '2019-12-01'
and o.odate < '2019-12-17'
Notes:
this makes use of the half-open strategy to compare the date, which is a good practice in this kind of situation.
this query would take advantage of an index on `orders(client_id, date)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I have two tables one named logins and one named members.
Both members and logins have "Name" field. This is the primary key.
Members includes information for accounts including the field "Rank"
logins includes information on last login with a unix time stamp on the field "time"
I need to run a query for the members table that is Rank > 1 but I also need to make sure that the "time" field is more than 1391212800 from the logins table.
How would I do this?
You can use a join to build a recordset from two distinct tables. The on clause allows you to specify the relation between the two tables.
Finally, you can apply a where clause to the new recordset that can refer to fields from both the original tables.
select *
from Members m
join Logins l
on l.name = m.name
where m.Rank > 1
and l.time > 1391212800
select m.name , m.rank from logins l
inner join members m
on l.mame = m.name
WHERE time >= 1391212800
and m.rank > 1
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I Have 3 tables,
Table senitems
Table inbox
Table outbox
how join into 1 tables, look at to this link for image illustration:
please help
You don't need a JOIN for this, what you need is UNION ALL:
SELECT
SendingDateTime AS DateTime,
DestinationNumber AS PhoneNumber,
SMS,
'sentitems' AS Folder
FROM sentitems
UNION ALL
SELECT
ReceivingDateTime, SenderNumber, SMS,
'inbox'
FROM inbox
UNION ALL
SELECT
InserDateTime, DestinationNumber, SMS,
'outbox'
FROM outbox;
Note that: The columns' names for the final result set will be inferred from the first query.
If you want to order the result set, put that query in a subquery and ORDER BY in the outer one, something like this:
SELECT *
FROM
(
SELECT
SendingDateTime AS DateTime,
DestinationNumber AS PhoneNumber,
SMS,
'sentitems' AS Folder
FROM sentitems
UNION ALL
SELECT
ReceivingDateTime, SenderNumber, SMS,
'inbox'
FROM inbox
UNION ALL
SELECT
InserDateTime, DestinationNumber, SMS,
'outbox'
FROM outbox
) AS sub
ORDER BY DateTime