php - How do I select the highest column value in an SQL query IF another column is equal to a certain string -


I am trying to set up a matching system that chooses the highest value of two separate questions. I have seen other answers on the site and they do not help, I want to compare it originally:

I want to get the highest value from the version column where < Code> type is equal to, for example "code> type1 ". I want to get the highest value from the version column, where is of type is equal to " type2 ". I want to compare these files with PHP and find out that type2 's version to type1 ' s version < More than / code>. Is there any way I can do that?

Edit: Brian Dimilia has answered this question. Has a moderator answered it?

I believe this will do the job you want:

  Select the version, type from tbl, where version = (maximum) (from tbl) select where type ('type1', 'type2'))  

Type Result column will show 'type1' if maximum (version) was associated with a type 1 record, or 'Type 2' if maximum (version) was connected to a type 2 record.

If you want to know the highest version for Type 1 and the highest version for Type 2 (as you want values ​​returned to the output), and identify which high Is, you can use the following:

  select type 1_max_version, T2. As the case type 2_max_version, case when t1.version & gt; T2.version then t1.version else t2.version end_machine_num as the end, when t1.version> T2.version then connect to tbl t1 as 'type1' else 'type2' end as the highest_version_type, join tbl t2, where t1.version = (select max from tbl) where type = 'type1') and t2 Version = (Select from maximum Tbl where type = 'type2')  

Depending on your comment, if you want to filter on rows where there were some values ​​of field called "URIs" , You can use it following. It also assumes that you want to apply this filter for both Type 1 and Type 2 line. I know that this is not the case. In addition, if you want to make additional fields in the WHEEE clause, you will have to add them to the join clause between T1 and T2, otherwise you will need to repeat twice and repeat once again in the WHERE clause. Choose T1 for T2

  Type 1_max_version as type, t2.version as type2_max_version, case when t1.version & gt; T2.version then t1.version else t2.version end_machine_num as the end, when t1.version> T2.version Then to connect 'firewall' and 'FBIpace' to TBL T1, TBL is T1 as the highest version of T2.usrips = t2.usrips, where t1.version = (TB to max (version ) Select where type = 'firewall' and 'URI'% 1.1.1.1% ') and T.Warson = (select maximum (version) from TB where type =' FBIpass' and '% 1.1.1.1%' Like URIs) and T1. '% 1.1.1.1%', such as spirits.  

See Bela:


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

c# - Create dynamic LINQ query expression at runtime which translates into a parameterized SQL query for nullable types -

cmake - cpack generates RPMs with %files entries that conflict with the RPM spec. How to fix? -