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
Solved

GROUP BY MySQL query question

Posted on 2014-03-18
3
398 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 143

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
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…
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.

829 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