Solved

GROUP BY MySQL query question

Posted on 2014-03-18
3
396 Views
Last Modified: 2014-04-05
I have a businesses database with duplicates. When i GROUP BY BusinessName in a City always the first result in the DB will appear. Example:

ID 1 - BusinessName A (old result with old address, phone ..)
ID 2 - BusinessName A (new result with new address, phone ..)

In this case ID 1 will be shown.

Now when I import a new business database with new businesses I would like to echo ID 2 instead of ID 1 because ID 2 has updated address, phone number and so on. How can this be done without removing ID 1 from DB?

The best solution would be to replace ID 1 with ID 2. The problem is each business has a unique ID which is indexed in search engines therefore removing the old ID 1 would hurt search engine traffic.

Thanks for some input!
0
Comment
Question by:seopti
3 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39937172
Why don't you reverse order the results based on the ID (I assume is incremental) so the newest is always first?

If you are just wanting the latest record, but selecting multiple companies in the same query then you would need a sub query, something like this

SELECT * FROM table
    WHERE  id IN (select max(id)
    from table
    group by company_name)
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39937188
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39937315
You might try an order clause like ORDER BY ID DESC.

Another option might be to add a column of type = TIMESTAMP.  MySQL will update this column with the current date / time value whenever the row changes.  So if you ORDER BY timestamp_column DESC you will always find the most recent value, even if the ID values change.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question