English 中文(简体)
短片询问
原标题:Shorten sql queries
  • 时间:2012-01-11 21:36:55
  •  标签:
  • mysql
  • sql

我提出了3个问题,要求重新排列我的桌子。

SELECT @i:=0;
UPDATE imagedata SET imagedata.rank = 0;
UPDATE imagedata SET imagedata.rank = (SELECT @i:=@i+1) WHERE imagedata.kategorieID = 1;

我从上至下执行。

问题: 这样做的越少?


thx for all the feedback.. but i have a differnet Idear now: Somehow I have to "connect" the "kategorieID" with the "id(Primary key)" collumn Therefore I need to store both information into the "rank" collumn in the format like:

cxxx

c=Category Number(0 to 4) xxx = id (1 to ...n)..unique!

考试:产出:

   rank
 +------+
 + 1001 +
 + 1002 +
 + 1003 +
 + ...  +
 + 1018 +
 + ...  +
 + 2001 +
 + 2002 +
 + 1019 +
 + 1020 +

到目前为止,我也这样做:

UPDATE imagedata SET imagedata.rank = (SELECT CONCAT(kategorieID,"",LPAD(id,3, 0 )) ) WHERE id=88;

一旦“等级”填满了数据,它就给我放弃2 先令。

Therefore I have to: 1) get "rank" collumn of id_1 and id_2 2) get the substring ( xxx ) of that string exampl: ("004" .. "012") 3) exchange substrings of id_1 "rank" and id_2 "rank"

SELECT @ix1:=SUBSTRING(rank, -3) FROM imagedata WHERE id=88;
SELECT @ix2:=SUBSTRING(rank, -3) FROM imagedata WHERE id=83;

。 我知道如何做到这一点,但真心知道如何交流?

最佳回答

我只想修改第二点,以更新第三行的WON T,如:

SELECT @i:=0;
UPDATE imagedata SET imagedata.rank = 0 WHERE imagedata.kategorieID <> 1;
UPDATE imagedata SET imagedata.rank = (SELECT @i:=@i+1) WHERE imagedata.kategorieID = 1;

http://www.un.org。 Per Lamak s comment, if the kategorieID 页: 1

SELECT @i:=0;
UPDATE imagedata SET imagedata.rank = 0 WHERE imagedata.kategorieID <> 1 OR imagedata.kategorieID IS NULL;
UPDATE imagedata SET imagedata.rank = (SELECT @i:=@i+1) WHERE imagedata.kategorieID = 1;

此外,这一解决办法可能比你更慢,特别是如果在<代码>kategorie上有一个指数。 我之所以这样说,是因为尽管你正在更新较少的浏览量,但现在必须首先在<条码>上过滤。 或许,你应首先测试这一业绩,看看它是否真正更快。

问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签