Can someone "break down" the syntax here. Please. I need to learn this ASAP.
From my limited experience -
firstname and lastname are columns and list is a table.
count(id)>1 is used to check if there is more than one row with the same...
That's it. I don't know what this does but I need to understand it.
SELECT firstname, lastname, list.address FROM list
INNER JOIN (SELECT address FROM list
GROUP BY address
HAVING count(id) > 1) dup
ON list.address = dup.address
This query will return a list of all names (first and last name), which contain a duplicate address. This part
SELECT address FROM list
GROUP BY address HAVING count(id) > 1
Gets a list of all the addresses that occur more than once in the table, This is then joined back to the table itself, to return all names which have the same address. This should return a list of all the unique address which have more than 1 name associated with them, along with the names that go along with the addresses.
Related
I have a Table containing columns Email, Ip, State, City, TimeStamp, Id
I need to count where Email and IP are distinct, group by State
So when I run a MYSQL query,
select State, City ,count(distinct( Email )), count(DISTINCT( IP))
from table
group by Stat, City
It gives me distinct of each, but not AND
I need a count of distinct Email && Distinct IP ; grouped by State, City.
And distincts cant be within the Group, it has to be the 1st instance of EMAIL, and first instance of IP in entire database. So if i expand it, and add a date parameter, even though im selecting a specific date, I still can check whole database for the uniques.
So if i need
select state, city, count ( distinct ( IP ) , count ( distinct ( EMAIL ))
from table
where timestamp > date(2014-12-01)
group by state, city
What type of query is this? And how can I accomplish this?
My gut tells me i need to do CONCAT as suggested, but also another select inside. So select whole database distinct ip, then select that specific criteria from the other select.
This can help a bit to have a "distinct(A && B)"
SELECT DISTINCT(CONCAT(A,'_',B)),C,D
FROM table
GROUP BY C,D
We struggled to do this on a production server and found the query required was too resource intensive. So we created a table with an update on first instance the item occurs, then we check for counts with a join like so:
select count(a.State) from tablename A
inner join table_update U
on a.id = u.id
WHERE a.parameters..
and c.first_email = 1
and c.first_ip = 1
We couldnt find a single table that wouldnt bring our server down with 400,000 records. Its not a classy answer, but its what we had to use.
I have two tables - clients and - group
I need to get county and zip from clients and group-assigned from group
When I search, I cannot get distinct results, that is, instead of the output showing 100 clients with zipcode 12345 in jones county in main st group.
I need to have each zip and county listed once by group. I have googled and attempted many ways but it is just beyond me.
Can anyone assist in steering me to the correct way
Adding GROUP BY group, city, zip to the end of your query should get you what you need. It will only return unique combinations of the three.
Presumably you have something like:
select g.*, c.county, c.zip
from clients c join groups g on <some join condition>
You want one result per group. So, add a group by clause such as:
group by g.id -- assuming id uniquely identifies each group
This will give an arbitrary value for the other fields, which may be sufficient for what you are doing. (This uses a MySQL features called Hidden Columns.)
I have a mysql table with the fields id, name, address.
Some entries have identical names but different addresses; some have identical addresses but different names.
I want to get a list including one entry at most for each unique address and one entry at most for each unique name.
If I could do a group by name then put the results in a temp table and then select again and do a group by address, then I would get exactly what i want - but that seems too complicated.
And doing a "group by name,address" will not give me what I am looking for - that would include entries for all different combinations of name,address whereas I want one entry for each name and one for each address. Thanks for your help!
There's probably a nicer way to do this, and I'm making some assumptions about what you want, but you could try something like this:
SELECT Name, '' AS Address, Count(Address) AS RowCount
FROM TableName
GROUP BY Name
UNION
SELECT '' AS Name, Address, Count(Name) AS RowCount
FROM TableName
GROUP BY Address
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.
Complete noob question, so apologies for that.
I have two tables, a members table with an email address and telephone number in it and a second table that will have email addresses and telephone numbers in it with many instances of the members' telephone number or email address. I want to query the second table and list all results corresponding to each member's email address or telephone number.
Thanks very much
Here's a rough query based on the info you supplied:
select members_table.*, joined_tables.*
from members_table,
((select * from second_table
join members_table
on members_table.email_address = second_table.email_address)
union /* or intersect if you don't want dupes */
(select * from second_table
join members_table
on members_table.telephone_number = second_table.telephone_number)
) joined_tables;
At least it should give you an idea on how to go about it.