how to find an amount of certain duplicates in mysql table? - mysql

So I have a table with persons.
This table connects with vehicles with manyToMany.
I need to find how many times does id appear on the table person_vehicle.
How can I do this?
Thanks everybody for asking

Here are two suggestion.
For all the names:
Gives number of times the names are repeated.
SELECT count(name) FROM persons GROUP BY name;
Specifically for the jake:
SELECT count(name) FROM persons WHERE name = "Jake";

What have you tried? Depending on the table, you can do something like:
SELECT * FROM persons WHERE first_name = 'Jake'
Which will return the number of rows with the first name of "Jake"
Alternatively, if you only want one row with the total count of occurrences:
SELECT COUNT(first_name) FROM persons WHERE first_name = 'Jake'

You're looking for GROUP BY.
A table contains multiple columns. You have to decide first which column(s) of the data that you need are in common.
Suppose I have a table address(person_name, plot_no, pin);
If I try to select, for a particulate pin, how many people are living there:
SELECT pin, COUNT(*)
FROM address
GROUP BY pin;
If I try to select, for a particulate plot_no and pin, how many people are living there:
SELECT plot_no, pin, COUNT(*)
FROM address
GROUP BY plot_no, pin;

Use group by statement on the column, on which you are looking for duplicates if the count comes more than 1, it means they are repeating.
Select count(id), name from table_name group by id HAVING count(id) > 1;
Only the duplicates records will come.

Try this:
SELECT count(name) FROM persons WHERE name = "Jake";

Related

MYSQL query to Select the first duplicate record in JAVA

I am trying to retrieve the the first row among the duplicate row, THE FIRST OCCURED ***
--Table--
Order_No Product User
1 Book Student
2 Book Student
3 Book Student
I want to get the Order_No of the first duplicate row in JAVA, I have used DISTINCT and DISTINCT TOP 1 etc but nothing worked, NEED HELP
SELECT min(order_no), product, user
FROM 'table'
GROUP BY user, product
This is basic SQL?
SELECT min(order_no), product, user FROM table GROUP BY product, user
See also more information on GROUP BY
All fields not part of your group by must have some sort of way to determine which to pick of the n potentially different values. min() will pick the lowest value (even with strings and dates) while max() will pick the highest. You can also use First() and Last() to grab the value according to when they show up.
Supposing you had other values to pick from, you might see something like:
SELECT min(order_no), product, user, min(creation_date),
sum(quantity), first(billing_address)
FROM orders GROUP BY product, user
SELECT t.*
FROM table t
WHERE NOT EXISTS ( SELECT a
FROM table t2
WHERE t2.Product = t.Product
AND t2.User = t.User
AND t2.Order_No < t.Order_No
)

MySQL beginner query

I'm having some trouble figuring out a query. I have 2 tables, COMPANY, which contains a company id column, comp_id, and a name column. The second table, SOURCE, has a column for comp_id and a column for parts the company sells, parts.
How can I write a query to find the names of companies that produce all parts? I've figured that this uses not exist statements, but i can't figure out how.
Thanks.
Try this...
SELECT c.name from company join source s on (s.comp_id = c.comp_id) where parts = [:part_id];
Select name
from company, source
where company.comp_id=source.comp_id;
SELECT name, count(*) AS parts_count
FROM company, source
WHERE company.comp_id = source.comp_id
GROUP BY name
HAVING parts_count = (select count(*) from source)
Is very simple and command, suppose a part is "875"
SELECT name FROM company WHERE comp_id IN(SELECT comp_id FROM parts
WHERE parts = 875)
this allow you have the same part in distinct companies
Select name from companies,source where companies.comp_id=source.comp_id;

MySQL GROUP BY and COUNT

I have a small problem regarding a count after grouping some elements from a mysql table,
I have an orders table .. in which each order has several rows grouped by a code (named as codcomanda) ... I have to do a query which counts the number of orders per customer and lists only the name and number of orders.
This is what i came up (this might be dumb ... i'm not a pro programmer)
SELECT a.nume, a.tel, (
SELECT COUNT(*) AS `count`
FROM (
SELECT id AS `lwtemp`
FROM lw_comenzi_confirmate AS yt
WHERE status=1 AND yt.tel LIKE **a.tel**
GROUP BY yt.codcomanda
) AS b
) AS numar_comenzi
FROM lw_comenzi_confirmate AS a
WHERE status=1
GROUP BY tel;
nume = NAME
tel = PHONE (which is the distinct identifier for clients since there's no login system)
The problem with the above query is that I don't know how to match the a.tel with the one on which the first select is on. If I replace it with a number that is in the db it works....
Can anyone help me one how to refer to that var?
or maybe another solution on how to get this done?
If any more info is needed I`ll provide asap.
Please, correct me if I'm wrong in my understanding of your schema:
lw_comenzi_confirmate contains nume and tel of the customer;
lw_comenzi_confirmate contains order details (same table);
one order can have several entries in the lw_comenzi_confirmate table, order is distinguished by codcomanda field.
First, I highly recommend reading about Normalisation and fixing your database design.
The following should do the job for you:
SELECT nume, tel, count(DISTINCT codcomanda) AS cnt
FROM lw_comenzi_confirmate
WHERE status = 1
GROUP BY nume, tel
ORDER BY nume, tel;
You can test this query on SQL Fiddle.

Combining count(*) with SQL statements into one table

I have two tables in my MySQL database (table1 and table 2). I want to write a SQL query that outputs some summary stats in a nicely formatted report.
Let's for an example consider a first SQL query that takes the users over 57 year of age from the first table
SELECT count(*) AS OlderThank57
FROM table1
WHERE age >57
And from the second table we want to get the number of users that are female
SELECT count(*) AS FemaleUsers
FROM table2
WHERE gender = "female"
Now I want to have an output like the following
Number of Felame users from table 2: 514
Number of users over the age of 57 from table 1: 918
What is the best way of generating such a report?
I would offer to expand one level from Adrian's answer... return as two separate fields so you could place them separately in a report, or align / format the number, etc
SELECT 'Number of Female users from table 2:' as Msg,
count(*) as Entries
FROM table1
WHERE age >57
UNION ALL
SELECT 'Number of users over the age of 57 from table 1:' as Msg,
count(*) as Entries
FROM table2
WHERE gender = "female"
You might have to force both "Msg" columns to the same padded length, otherwise one might get truncated. Again, just another option...
You could always try the WITH ROLLUP directive when using a GROUP BY:
SELECT COUNT(*), gender FROM table1 GROUP BY gender WITH ROLLUP
If you want to get a bit crazy you can always make a series of IFs that handles the logic for one or more thing at a time:
SELECT COUNT(*) IF(gender='female', 'female', IF(age>57, 'older_than_57', '*')) AS switch FROM table1 GROUP BY switch WITH ROLLUP
SELECT CONCAT('Number of users over the age of 57 from table 1:', count(*))
FROM table1
WHERE age >57
UNION ALL
SELECT CONCAT('Number of Felame users from table 2: ', count(*))
FROM table2
WHERE gender = "female"
I don't have mysql database to check it, so you might have to cast count(*) to string.
A union is your only option if you don't have a normalized database. Your other option is to better standardize/normalize your db so you can run much more efficient queries.

mysql group by and filtering the values in each grouped record

i have a table of users im grouping by age, but each user also has a nationality and if one of the users nationality is US i want that to be value in the group record, currently it seems to take the first nationality it finds, how can i write this query?
One way to do it would be:
SELECT *, IF(INSTR(GROUP_CONCAT('--', nationality, '--'), '--US--'),
'US', nationality)
FROM table GROUP BY age;
What this does is that GROUP_CONCAT combines all the nationalities of one age and if it finds the string 'US' among them, it returns 'US' and otherwise it returns the nationality as it would normally do. It also adds '--' in the beginning and end of a nationality to make 'US' become '--US--'. If you didn't do that, the query would also think that any other nationality which contains the consecutive characters 'US' would mean US. But those '--' characters are only used internally and are not shown in the final result.
Edit: Another (cleaner but longer) way came into my mind:
SELECT * FROM (SELECT * FROM table WHERE nation='US'
UNION
SELECT * FROM table WHERE nation!='US') AS tmp
GROUP BY age;
So, first select persons whose nationality is US, then select persons whose nationality is not US and combine those two sets of persons so you get a table of persons in an order where there are first persons who are from US and then others. Then perform the GROUP BY operation to that table and you'll always get the nationality to be US if there's at least one person from US in that age, because it will always come first.