MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 11th, 2012, 01:47 AM
TASB TASB is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2009
Posts: 155 TASB User rank is Second Lieutenant (5000 - 10000 Reputation Level)TASB User rank is Second Lieutenant (5000 - 10000 Reputation Level)TASB User rank is Second Lieutenant (5000 - 10000 Reputation Level)TASB User rank is Second Lieutenant (5000 - 10000 Reputation Level)TASB User rank is Second Lieutenant (5000 - 10000 Reputation Level)TASB User rank is Second Lieutenant (5000 - 10000 Reputation Level)TASB User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 8 h 9 m 7 sec
Reputation Power: 88
Reindexing field in table

Hello.

I have topics table.

Code:
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| topic_ID    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| nav_ID      | int(10) unsigned | NO   |     | NULL    |                |
| topic_index | int(10) unsigned | NO   |     | NULL    |                |
| topic_name  | varchar(100)     | NO   |     | NULL    |                |
+-------------+------------------+------+-----+---------+----------------+


The topic_index field is used to store sub indexing of the topics which belong to a specific nav_id. Users have the ability move topics up or down in the list.

There is one particular nav_index value where I want the topics to be indexed alphabetically.

Writing a PHP routine to reorder the topic_index field would be simple enough but I would like to achive the task within MySQL.

So in essence if you query the table;

select * from om_topics where nav_id=3 order by topic_name asc;

I wold like to update topic_index = rowNum from the previous select query.

Reply With Quote
  #2  
Old October 11th, 2012, 02:00 AM
TASB TASB is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2009
Posts: 155 TASB User rank is Second Lieutenant (5000 - 10000 Reputation Level)TASB User rank is Second Lieutenant (5000 - 10000 Reputation Level)TASB User rank is Second Lieutenant (5000 - 10000 Reputation Level)TASB User rank is Second Lieutenant (5000 - 10000 Reputation Level)TASB User rank is Second Lieutenant (5000 - 10000 Reputation Level)TASB User rank is Second Lieutenant (5000 - 10000 Reputation Level)TASB User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 8 h 9 m 7 sec
Reputation Power: 88
Thanks,

I figured it out.


Code:
update
	om_topics t, 
	(select 
		@rownum:=@rownum+1 'rank', 
		om_topics.topic_id 
	from 
		om_topics, 
		(SELECT @rownum:=0) r 
	where 
		nav_ID=3 
	order by 
		topic_name asc) r 
set 
	t.topic_index = r.rank 
where 
	t.topic_ID=r.topic_ID;

Last edited by TASB : October 11th, 2012 at 02:03 AM.

Reply With Quote
  #3  
Old October 12th, 2012, 12:36 PM
rdubya rdubya is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 4 rdubya User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 23 m 22 sec
Reputation Power: 0
Thanks for the help TASB. I think I am getting close but I am still not quite there... any help would be most appreciated:



UPDATE PlayerRankings p,
(SELECT @rownum := @rownum + 1,
@rank := IF(@prev_val!=Score,@rownum,@rank) AS rank,
@prev_val := Score
FROM
p,
(SELECT ((Select @rownum := 0),
(SELECT @rank := 1) ,
(SELECT @prev_val = NULL)) AS ra)
WHERE
idGame = 1
ORDER
BY Score DESC)
SET p.Rank = ra.rank;


Error Code: 1248. Every derived table must have its own alias 0.015 sec


I looked up the error but I don't think I grasp the concept of it with this.

Thanks,
Ryan

Reply With Quote
  #4  
Old October 12th, 2012, 01:02 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 43rd Plane (26000 - 26499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,444 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 14 h 25 m 12 sec
Reputation Power: 4141
Quote:
Originally Posted by rdubya
I looked up the error but I don't think I grasp the concept of it with this.
a derived table is a subquery in the FROM clause

here's a derived table with an alias --

(SELECT @prev_val = NULL) AS ra

here's one without --

(Select @rownum := 0)
Comments on this post
rdubya agrees!
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #5  
Old October 12th, 2012, 04:02 PM
rdubya rdubya is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 4 rdubya User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 23 m 22 sec
Reputation Power: 0
Hmm.. I tried setting all the tables as derived:::

UPDATE PlayerRankings p,
(SELECT @rownum := @rownum + 1 AS rownumber,
@rank := IF(@prev_val!=Score,@rownum,@rank) AS rank ,
@prev_val := Score AS score
FROM
p,
(SELECT ((Select @rownum := 0 AS test2) ,
(SELECT @rank := 1 AS test) ,
(SELECT @prev_val = NULL AS prevscore)) AS ra)
WHERE
p.Game_idGame_FK = 1
ORDER
BY p.Score DESC)
SET p.Rank = ra.rank;

Still getting the same error.... Am I not setting them properly?

Reply With Quote
  #6  
Old October 12th, 2012, 05:04 PM
TASB TASB is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2009
Posts: 155 TASB User rank is Second Lieutenant (5000 - 10000 Reputation Level)TASB User rank is Second Lieutenant (5000 - 10000 Reputation Level)TASB User rank is Second Lieutenant (5000 - 10000 Reputation Level)TASB User rank is Second Lieutenant (5000 - 10000 Reputation Level)TASB User rank is Second Lieutenant (5000 - 10000 Reputation Level)TASB User rank is Second Lieutenant (5000 - 10000 Reputation Level)TASB User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 8 h 9 m 7 sec
Reputation Power: 88
Quote:
Originally Posted by rdubya
(SELECT ((Select @rownum := 0 AS test2) ,
(SELECT @rank := 1 AS test) ,
(SELECT @prev_val = NULL AS prevscore)) AS ra)


I think your problem lies here.

Try

Code:
(SELECT 
	@rownum := 0 AS test2,
	@rank := 1 AS test,
	@prev_val = NULL AS prevscore) AS ra;

Reply With Quote
  #7  
Old October 12th, 2012, 07:13 PM
rdubya rdubya is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 4 rdubya User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 23 m 22 sec
Reputation Power: 0
Same error =( thanks for trying though-- any other ideas?


UPDATE PlayerRankings p,
(SELECT @rownum := @rownum + 1 AS rownum,
@rank := IF(@prev_val!=Score,@rownum,@rank) AS rank ,
@prev_val := Score AS score
FROM
p,
(SELECT @rownum := 0 AS test2,
@rank := 1 AS test,
@prev_val = NULL AS prevscore)AS ra
WHERE
p.Game_idGame_FK = 1
ORDER
BY p.Score DESC)
SET p.Rank = ra.rank;

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Reindexing field in table

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap