Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

GROUP BY MySQL query question

Posted on 2014-03-18
3
Medium Priority
?
409 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 111

Accepted Solution

by:
Ray Paseur earned 2000 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …
Suggested Courses

597 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