SQL从表

时间:2015-10-14 08:10:35

标签: php mysql sql distinct

我的桌子有很多行(> 10K)。大多数行都具有与ques_id关联的重复角色值。我是sql的新手。 我要做的是选择不同的AND行添加最新的ques_id。 这是我的表(tbl_questions)结构。

id | ques_id | question       | ans
1  |  2      | HTML stands..  |  3 
2  |  5      | PHP stands..   |  2 
3  |  6      | CSS stands..   |  4 
4  |  6      | CSS stands..   |  4
5  |  5      | PHP stands..   |  2
6  |  6      | CSS stands..   |  4

这将是理想的结果:

id | ques_id | question       | ans
1  |  2      | HTML stands..  |  3 
5  |  5      | PHP stands..   |  2
6  |  6      | CSS stands..   |  4

以下是我迄今为止尝试过的查询:

SELECT DISTINCT ques_id, question, ans FROM tbl_questions

4 个答案:

答案 0 :(得分:2)

通过按组给出行号,只是另一种观点。

<强>查询

bool pop(T&)

SQL Fiddle

答案 1 :(得分:0)

您想要每个<iframe width="854" height="480" style="display:none;" src="https://www.youtube.com/embed/yourVideoID?autoplay=1&loop=1&playlist=yourVideoID" frameborder="0" allowfullscreen></iframe>的最新行吗?您可以使用question返回这些行:

NOT EXISTS

答案 2 :(得分:0)

SELECT a.* 
  FROM tbl_questions a 
  JOIN 
     ( SELECT ques_id
            , MAX(id) max_id 
         FROM tbl_questions 
        GROUP 
           BY ques_id
     ) b 
    ON b.max_id = a.id;

答案 3 :(得分:0)

尝试此查询

SELECT 
SUBSTRING_INDEX(GROUP_CONCAT(id ORDER BY id DESC),',',1) AS i_d, 
ques_id, 
question,
SUBSTRING_INDEX(GROUP_CONCAT(ans ORDER BY id DESC),',',1) AS Answer 
FROM tbl_questions 
GROUP BY ques_id

输出

i_d |ques_id | question       | Answer
1    2      HTML stands..       3
5    5      PHP stands..        2
6    6      CSS stands..        4