Solved

GROUP BY MySQL query question

Posted on 2014-03-18
3
402 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 143

Expert Comment

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

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

Webinar: MongoDB® Index Types

Join Percona’s Senior Technical Services Engineer, Adamo Tonete as he presents “MongoDB Index Types, How, When and Where Should They be Used?” on Wednesday, July 12, 2017 at 11:00 am PDT / 2:00 pm EDT (UTC-7).

Question has a verified solution.

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

Creating and Managing Databases with phpMyAdmin in cPanel.
This article discusses four methods for overlaying images in a container on a web page
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

724 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