选择不同的和最新的

时间:2016-03-04 15:10:13

标签: php mysql sql

我正在尝试获取最近的bahis.ID和bahis.isim字段女巫有一个JOIN规则。但在这一个我得到了相同的bahis.IDs和bahis.isims。我想得到他们最近的那个,然后继续。

我尝试了很多,但他们都以某种方式失败了。这是我最近的最后一个问题;

app.factory("Auth", ["$firebaseAuth",function($firebaseAuth) {
var ref = new Firebase("https://myfirebaseapp.firebaseio.com");
return $firebaseAuth(ref);
}]);

$scope.login = function() {
    $scope.authObj.$authWithPassword({
      name: $scope.data.name,
      email: $scope.data.email,
      password: $scope.data.password
    }).then(function(authData) {
        authenticated = true;
        console.log("Logged in as:", authData.uid, authenticated);
        $location.path("profile");
        check();
    }).catch(function(error) {
        authenticated = false;
        console.error("Authentication failed:", error);
        check();
    });
}

简单地说,我该怎么做?

结果:

SELECT bahis.ID, bahis.isim 
FROM bahis 
JOIN yorumbahis 
  ON yorumbahis.bahisid = bahis.ID 
ORDER BY yorumbahis.ID DESC LIMIT 0,12

预期结果;

74 dfgfdggdf5455
68 sdffcc33
68 sdffcc33
76 adsadsd333
76 adsadsd333
74 dfgfdggdf5455
86 hjjk khjjk
73 cdsc4344
63 aaaaxxxxsssxxx
76 adsadsd333
76 adsadsd333
76 adsadsd333

2 个答案:

答案 0 :(得分:1)

使用此示例

CREATE TABLE bets        (`ID` int, `name` varchar(14)); 

INSERT INTO bets (`ID`, `name`) 
VALUES 
(1, 'a'),     (2, 'b'), 
(3, 'c'),     (4, 'd'), 
(5, 'e'); 

CREATE TABLE comments    (`ID` int, `betid` varchar(14)) 
; 

INSERT INTO comments     (`ID`, `betid`) 
VALUES 
(1, '2'),     (2, '2'), 
(3, '3'),     (4, '1'), 
(5, '4'),     (1, '3'), 
(2, '4'),     (3, '1'), 
(4, '2'),     (5, '2');

<强> Sql Fiddle Demo

SELECT b.name, MAX(c.id)
FROM bets b 
JOIN comments c 
ON b.`ID` = c.`betid`
GROUP BY b.name
ORDER BY MAX(c.id) DESC;

<强>输出

| name | MAX(c.id) |
|------|-----------|
|    b |        10 |
|    a |         8 |
|    d |         7 |
|    c |         6 |

答案 1 :(得分:0)

SELECT bahis.ID, max(bahis.isim) 
FROM bahis 
JOIN yorumbahis 
  ON yorumbahis.bahisid = bahis.ID 
group by bahis.id
ORDER BY yorumbahis.ID DESC LIMIT 0,12