CodeCharge Studio
search Register Login  

Visual PHP Web Development

Visually Create Internationalized Web Applications, Web Reports, Calendars, and more.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> General/Other

 serial number with condition

Print topic Send  topic

Author Message
itzumar

Posts: 88
Posted: 10/12/2013, 1:41 AM

dear all!

i have 6 rows in mysql table as follow

city student
mzd umar
mzd ali
mzd ahsan
bagh john
bagh saad
bagh king

i want to genrate serial number for my table . but i want serial number repeated when city name is chnaged. it should look like that

city student s.no
mzd umar 1
mzd ali 2
mzd ahsan 3
bagh john 1
bagh saad 2
bagh king 3

_________________
Umar
View profile  Send private message
Fishertoy

Posts: 9
Posted: 10/17/2013, 1:53 PM

Hello, first thing is that you need to add an autonumeric field being a primary key for the table (id in this example).

then just use this query.

SELECT
my_table.id,
my_table.city,
my_table.student,
(SELECT
COUNT(*)
FROM
my_table t2
WHERE
t2.city = my_table.city AND
t2.id <= my_table.id) sno
FROM
my_table



regards

View profile  Send private message
itzumar

Posts: 88
Posted: 10/18/2013, 4:57 AM

Dear fisher boy!

lot of thanks for your support. you almost solve my problem. but the problem is what can i do if city was not in order. for example if

city student s.no
mzd umar 1
bagh ali 1
mzd ahsan 2
bagh john 2
bagh saad 3
mzd king 3

than what i do? u almost solve 95%. plz guide me further
_________________
Umar
View profile  Send private message
Fishertoy

Posts: 9
Posted: 10/18/2013, 8:44 AM

SELECT
my_table.id,
my_table.city,
my_table.student,
(SELECT
COUNT(*)
FROM
my_table t2
WHERE
t2.city = my_table.city AND
t2.id <= my_table.id) sno
FROM
my_table
ORDER BY
my_table.city,
my_table.id



Regards
View profile  Send private message

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

PHP Reports

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.