473,386 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

select values from the 2 different table

123 100+
Sample tables (company_id RK companies.id):

Companies
id company
1 …
2 …
3 …

Locations
id company_id location revision
1 1 … 1
2 2 … 1
3 1 … 2
4 1 … 3
5 2 … 2
6 1 … 4

How to write an SQL query to select values from the following columns (companies.company, locations.location) where locations.revision value will be the highest for each company

I try
Select distinct companies.company, locations.location from Companies, Locations where companies.id=Locations.company_id order by locations.revision

but it not works..

How? thanks.
Jul 8 '08 #1
4 3791
coolsti
310 100+
This will work if you have a new enough mysql that allows subqueries:

Expand|Select|Wrap|Line Numbers
  1. select Companies.id, Locations.location, revision 
  2. from Companies inner join Locations on Companies.id=Locations.id 
  3. inner join 
  4. (select C1.id, max(C2.revision) as maxrevision 
  5. from Companies C1 inner join Locations C2 on C1.id=C2.id group by C1.id) T2
  6. on Companies.id=T2.id and revision=maxrevision
  7.  
Try to understand how this works, it is very useful. What you do is join your two tables, but then you get a row for every possible value of revision. So you need a "group by" (not a distinct). But the problem is, when you do the grouping, you want to extract the row that corresponds to the maximum revision for each company, not just the first row that appears in the join.

So to do this, you create the table T2 that appears in the subquery. Here, T2 consists of the companies "id" field, and the max(revision) which I give the alias maxrevision. In this subquery, table T2 is "grouped by" the company ID, and this causes the table to have rows which give the maximum value for the revision for each company ID.

So now you just need to join the first two joined tables with the table T2 created in the subquery, using the two equalities "Companies.id=T2.id" and "revision=maxrevision" because you only want the rows in your result that correspond to the maximum value of revision for each company.
Jul 8 '08 #2
perhapscwk
123 100+
ThaNKS SO MUCH. thanks.


This will work if you have a new enough mysql that allows subqueries:

Expand|Select|Wrap|Line Numbers
  1. select Companies.id, Locations.location, revision 
  2. from Companies inner join Locations on Companies.id=Locations.id 
  3. inner join 
  4. (select C1.id, max(C2.revision) as maxrevision 
  5. from Companies C1 inner join Locations C2 on C1.id=C2.id group by C1.id) T2
  6. on Companies.id=T2.id and revision=maxrevision
  7.  
Try to understand how this works, it is very useful. What you do is join your two tables, but then you get a row for every possible value of revision. So you need a "group by" (not a distinct). But the problem is, when you do the grouping, you want to extract the row that corresponds to the maximum revision for each company, not just the first row that appears in the join.

So to do this, you create the table T2 that appears in the subquery. Here, T2 consists of the companies "id" field, and the max(revision) which I give the alias maxrevision. In this subquery, table T2 is "grouped by" the company ID, and this causes the table to have rows which give the maximum value for the revision for each company ID.

So now you just need to join the first two joined tables with the table T2 created in the subquery, using the two equalities "Companies.id=T2.id" and "revision=maxrevision" because you only want the rows in your result that correspond to the maximum value of revision for each company.
Jul 9 '08 #3
perhapscwk
123 100+
thanks.

But your query will return 3 column, how to just get
2 column? Companies.id, Locations.location
Jul 9 '08 #4
coolsti
310 100+
Hi,

yes, just try to understand what is going on, then you can modify this query structure to many possible situations.

The query is composed basically of two parts. The first part does the select as you wish to see the results, meaning it gives you the columns that you wish to see:
select Companies.id, Locations.location, revision
from Companies inner join Locations on Companies.id=Locations.id
and the second part of the query is building up this extra table, which I call T2 here, in order to allow you to select only the rows from the first part of the query that match your desired criteria, in this case "for each company, the row with the maximum revision":
inner join
(select C1.id, max(C2.revision) as maxrevision
from Companies C1 inner join Locations C2 on C1.id=C2.id group by C1.id) T2
on Companies.id=T2.id and revision=maxrevision
So if you want to have only two columns in your result instead of three, modify the first part of the query, to this:
select Companies.id, Locations.location
from Companies inner join Locations on Companies.id=Locations.id
Here I assumed you wish to have only the company ID and the location in your results table. You can add or remove columns in this part of the query as you desire.
Jul 9 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Jeff Sandler | last post by:
I need a MySQL select statement as part of a PHP script. I want to find rows where a certain column either starts with or equals a user-supplied string. The string will be 1 or more characters in...
3
by: Ian T | last post by:
Hi, I've got what I think (probably incorrectly) should be a simple SELECT : Two colums with data like col1 col2 1 50 1 51 2 50
7
by: Sunny K | last post by:
Hi guys, whilst working on a project which I thought was nearly complete I have come across a problem which was some how over seen, which I am hoping one of you guys know how to resovle. ...
5
by: Paul Reddin | last post by:
Hi, using ROWNUMBER() is affecting the plan of a view very badly... is there a way of writing the following view to ensure rownumber() is done as the last thing done? i.e after the calling...
3
by: Shelby Cain | last post by:
The select statements return different data for most_commons_vals depending on whether n_distinct is included in the select clause or not. I only seem to get the behavior below against int8...
1
by: Grant McLean | last post by:
Hi First a simple question ... I have a table "access_log" that has foreign keys "app_id" and "app_user_id" that reference the "application_type" and "app_user" tables. When I insert into...
4
by: satish | last post by:
Values of two columns in two different tables--presentation using select Hi Everyone, i have two tables in the database . One is called address table and one is adressPhone Table. Below...
1
by: Zvonko | last post by:
Hi! This is my table: CREATE TABLE ( sifrob VARCHAR(13), katbroj VARCHAR(15), kol FLOAT )
13
by: PinkBishop | last post by:
I am using VS 2005 with a formview control trying to insert a record to my access db. The data is submitted to the main table no problem, but I need to carry the catID to the bridge table...
8
by: crayfiss | last post by:
Hi, firstly I am a total freshie in all this. From what I have gathered on the web and this forum, I finally managed to get my form up. I have a set of radio buttons with values to it and a select...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.