-1

I need to group my table, my table had lot of columns but i am showing only two column jtcno and projectno. The primary key is roeid, it contain an auto increment numbers. My table structure is as given below

+-------+-----------+
| jtcno | projectno |
+-------+-----------+
| 8229  |     10636 |
| 8230  |     10636 |
| 8231  |     10636 |
| 8229  |     10637 |
| 8230  |     10637 |
| 8231  |     10637 |
| 8229  |     10637 |
| 8230  |     10637 |
| 8231  |     10637 |
+-------+-----------+

I need to group the table according to jtcno as follows

+-------+-----------+
| jtcno | projectno |
+-------+-----------+
| 8229  |     10636 |
| 8230  |     10636 |
| 8231  |     10636 |
| 8229  |     10637 |
| 8230  |     10637 |
| 8231  |     10637 |
+-------+-----------+

Can anyone help me to write the query. Thank you in advance.

Alvin John Babu
  • 1,710
  • 2
  • 16
  • 26

3 Answers3

1

This is the query you seem to want here:

SELECT DISTINCT jtcno, projectno
FROM yourTable
ORDER BY projectno, jtcno;
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

Try this query:

select distinct jtcno, projectno from tablename order by jtcno
Purvi Barot
  • 241
  • 2
  • 9
0

This will give you the desired result, you need to group by both the columns

  1. If there no index
SELECT DISTINCT jtcno, projectno
FROM my_table
ORDER BY jtcno, projectno;
  1. If there an index
SELECT jtcno, projectno
FROM my_table
GROUP BY jtcno, projectno;
aRvi
  • 2,203
  • 1
  • 14
  • 30
  • 1
    There is no problem in sql for which the above is a valid solution. In the absence of any aggregating functions, a GROUP BY clause is never appropriate. If you want DISTINCT results, use DISTINCT. – Strawberry Sep 27 '20 at 09:35
  • @Strawberry I have corrected my answer, I was not knowing this – aRvi Sep 27 '20 at 09:41
  • @Strawberry AS far as I have read `DISTINCT` is preferable if there is no index and `GROUP BY` is preferable if there is an index. https://stackoverflow.com/questions/581521/whats-faster-select-distinct-or-group-by-in-mysql – aRvi Sep 27 '20 at 09:45
  • Copy of my answer. Your original query was incorrect. – Tim Biegeleisen Sep 27 '20 at 12:38