doujiao0110 2012-02-18 06:54
浏览 37

如何检查各列中mysql查询的最后两个条目是否具有相同的值?

i have a table link with a structure like this

spreader | host1 | host2 | host 3 | id | datetime |
---------------------------------
expl.com | 1     | 5     | 2
what.de  | 1     | 5     | 2
expl.com | 1     | 2     | 3
what.de  | 2     | 5     | 5

How to check if the last 2 entry with the same spreader have the same values in host1, host2, host3 ...

For this example it sould give me

  • expl.com => host1 = 1
  • what.de => host2 = 5

I maybe know a way but i would have to have 10 sql querys one for each host. Do you know a efficient way to do this in php after i just get the entire date from the database with just one query?

Well i got this now it fires out 10 mysql querys i would love to see how to make this better:

global $hosters;
global $filespreader_names;

foreach ( $filespreader_names as $filespreader ) {
    $feedtable = feedback_tabelle_holen(" WHERE filespreader='$filespreader' ORDER BY aktualisiert DESC LIMIT 2");
    //var_dump($feedtable);
    foreach ( $hosters as $host ) {
        if ( ( $feedtable[0][$host] == $feedtable[1][$host] ) && ( $feedtable[0][$host] != 0 ) ) {
            echo "<br />$filespreader -> $host = " . $feedtable[0][$host];
        }
    }
}

the globals i get in are just arrays with all the spreaders and host names i need

  • 写回答

1条回答 默认 最新

  • dongxun7301 2012-02-20 15:27
    关注

    You might be able to do an Inner Join to produce matching records. If the gloabals you get in are from another table an inner join will give you a recordset with all the matching records that you could manipulate from there but it would only be one query.

    If this is TableA on the left and TableB on the right.
    
    id name       id  name
    -- ----       --  ----
    1  Pirate     1   Rutabaga
    2  Monkey     2   Pirate
    3  Ninja      3   Darth Vader
    4  Spaghetti  4   Ninja
    
    SELECT * FROM TableA
    INNER JOIN TableB
    ON TableA.name = TableB.name
    
    That query will result in rows of the  matching data according to your query
    
    id  name       id   name
    --  ----       --   ----
    1   Pirate     2    Pirate
    3   Ninja      4    Ninja
    

    I don't know if that helps at all but I wasn't really clear on your structure and exactly what you wanted. If the globals are session variables and not stored in a table this answer probably won't help you.

    If you want to use joins check out http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html

    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line