I use the database mysql v.4.
My problem is...
I have a select like:
select * from user where language = "de";
the result are then:
id name
=============== ====
1 max
5 tim
99 otto
so and what i need is a virtuell row (pos) with a counting index of the result:
id name pos
=============== ==== ===
1 max 1
5 tim 2
99 otto 3
how can i make this with mysql?
My first idea was to made that with views but mysql can't do that!
MFG Alexander W. 16 1735
<wa******@web.d e> wrote in message I have a select like: select * from user where language = "de";
the result are then: id name =============== ==== 1 max 5 tim 99 otto
so and what i need is a virtuell row (pos) with a counting index of the
result: id name pos =============== ==== === 1 max 1 5 tim 2 99 otto 3
how can i make this with mysql? My first idea was to made that with views but mysql can't do that!
There might be some special function, I don't know for sure. But in 5.0
they will have cursors, and in the present version you can always do it in
C++ and other languages.
I can think of trying an extra table, but it doesn't seem like the best
solution.
create table temp_product (
id int(1) unsigned auto_increment,
product_id int(1) unsigned not null references product(id),
primary key (id)
);
....
delete from temp_product;
insert into temp_product (product_id)
select id from product
order by name desc;
select temp_product.id , product.*
from product inner join temp_product on product.id =
temp_product.pr oduct_id;
<wa******@web.d e> wrote in message I have a select like: select * from user where language = "de";
the result are then: id name =============== ==== 1 max 5 tim 99 otto
so and what i need is a virtuell row (pos) with a counting index of the
result: id name pos =============== ==== === 1 max 1 5 tim 2 99 otto 3
how can i make this with mysql? My first idea was to made that with views but mysql can't do that!
There might be some special function, I don't know for sure. But in 5.0
they will have cursors, and in the present version you can always do it in
C++ and other languages.
I can think of trying an extra table, but it doesn't seem like the best
solution.
create table temp_product (
id int(1) unsigned auto_increment,
product_id int(1) unsigned not null references product(id),
primary key (id)
);
....
delete from temp_product;
insert into temp_product (product_id)
select id from product
order by name desc;
select temp_product.id , product.*
from product inner join temp_product on product.id =
temp_product.pr oduct_id;
<wa******@web.d e> wrote in message
news:b1******** *************** ***@posting.goo gle.com... I use the database mysql v.4.
My problem is... I have a select like: select * from user where language = "de";
the result are then: id name =============== ==== 1 max 5 tim 99 otto
so and what i need is a virtuell row (pos) with a counting index of the
result: id name pos =============== ==== === 1 max 1 5 tim 2 99 otto 3
Assume the following table:
CREATE TABLE student (
s_id int(11) NOT NULL auto_increment,
name varchar(15),
PRIMARY KEY (s_id)
);
Add some rows and delete (just to convice you this works in all cases
independent of the primary key)
insert into student (name) values ('tom');
insert into student (name) values ('bob');
insert into student (name) values ('sue');
insert into student (name) values ('mike');
insert into student (name) values ('joe');
insert into student (name) values ('zoe');
insert into student (name) values ('harpo');
delete from student where name = 'bob';
Now, note mc is the row count...indepen dent of s_id;
select a.name,sum(1) as mc
from student a, student b
where a.s_id <= b.s_id
group by a.s_id, a.name order by mc;
+-------+------+
| name | mc |
+-------+------+
| harpo | 1 |
| zoe | 2 |
| joe | 3 |
| mike | 4 |
| sue | 5 |
| tom | 6 |
+-------+------+
6 rows in set (0.00 sec)
Reference (TIP 22) http://osdn.dl.sourceforge.net/sourc...ADME_mysql.txt
Hope this helps,
Mike Chirico
<wa******@web.d e> wrote in message
news:b1******** *************** ***@posting.goo gle.com... I use the database mysql v.4.
My problem is... I have a select like: select * from user where language = "de";
the result are then: id name =============== ==== 1 max 5 tim 99 otto
so and what i need is a virtuell row (pos) with a counting index of the
result: id name pos =============== ==== === 1 max 1 5 tim 2 99 otto 3
Assume the following table:
CREATE TABLE student (
s_id int(11) NOT NULL auto_increment,
name varchar(15),
PRIMARY KEY (s_id)
);
Add some rows and delete (just to convice you this works in all cases
independent of the primary key)
insert into student (name) values ('tom');
insert into student (name) values ('bob');
insert into student (name) values ('sue');
insert into student (name) values ('mike');
insert into student (name) values ('joe');
insert into student (name) values ('zoe');
insert into student (name) values ('harpo');
delete from student where name = 'bob';
Now, note mc is the row count...indepen dent of s_id;
select a.name,sum(1) as mc
from student a, student b
where a.s_id <= b.s_id
group by a.s_id, a.name order by mc;
+-------+------+
| name | mc |
+-------+------+
| harpo | 1 |
| zoe | 2 |
| joe | 3 |
| mike | 4 |
| sue | 5 |
| tom | 6 |
+-------+------+
6 rows in set (0.00 sec)
Reference (TIP 22) http://osdn.dl.sourceforge.net/sourc...ADME_mysql.txt
Hope this helps,
Mike Chirico
"Mike Chirico" <mc******@comca st.net> wrote in message
news:L66dncvnK4 TEdAfdRVn- select a.name,sum(1) as mc from student a, student b where a.s_id <= b.s_id group by a.s_id, a.name order by mc;
That's pretty cool!
Reference (TIP 22) http://osdn.dl.sourceforge.net/sourc...ADME_mysql.txt
"Mike Chirico" <mc******@comca st.net> wrote in message
news:L66dncvnK4 TEdAfdRVn- select a.name,sum(1) as mc from student a, student b where a.s_id <= b.s_id group by a.s_id, a.name order by mc;
That's pretty cool!
Reference (TIP 22) http://osdn.dl.sourceforge.net/sourc...ADME_mysql.txt
"Siemel Naran" <Si*********@RE MOVE.att.net> wrote in message
news:Vw******** ************@bg tnsc05-news.ops.worldn et.att.net... "Mike Chirico" <mc******@comca st.net> wrote in message news:L66dncvnK4 TEdAfdRVn-
select a.name,sum(1) as mc from student a, student b where a.s_id <= b.s_id group by a.s_id, a.name order by mc;
That's pretty cool!
Reference (TIP 22) http://osdn.dl.sourceforge.net/sourc...ADME_mysql.txt
It's also possible to compute running aggregates. Assume the following
table
and data:
CREATE TABLE dice (
d_id int(11) NOT NULL auto_increment,
roll int,
PRIMARY KEY (d_id)
);
insert into dice (roll) values (1);
insert into dice (roll) values (2);
insert into dice (roll) values (3);
insert into dice (roll) values (4);
insert into dice (roll) values (5);
insert into dice (roll) values (6);
The following gives a running, cumulative, sum:
mysql> select a.roll, sum(b.roll) from dice a, dice b
where b.d_id <= a.d_id group by a.d_id, a.roll;
+------+-------------+
| roll | sum(b.roll) |
+------+-------------+
| 1 | 1 |
| 2 | 3 |
| 3 | 6 |
| 4 | 10 |
| 5 | 15 |
| 6 | 21 |
+------+-------------+
6 rows in set (0.00 sec)
To get a running average change the
sum to avg as follows:
mysql> select a.roll, avg(b.roll) from dice a, dice b
where b.d_id <= a.d_id group by a.d_id, a.roll;
+------+-------------+
| roll | avg(b.roll) |
+------+-------------+
| 1 | 1.0000 |
| 2 | 1.5000 |
| 3 | 2.0000 |
| 4 | 2.5000 |
| 5 | 3.0000 |
| 6 | 3.5000 |
+------+-------------+
6 rows in set (0.00 sec)
Regards,
Mike Chirico
Reference (TIP 23) http://osdn.dl.sourceforge.net/sourc...ADME_mysql.txt
"Siemel Naran" <Si*********@RE MOVE.att.net> wrote in message
news:Vw******** ************@bg tnsc05-news.ops.worldn et.att.net... "Mike Chirico" <mc******@comca st.net> wrote in message news:L66dncvnK4 TEdAfdRVn-
select a.name,sum(1) as mc from student a, student b where a.s_id <= b.s_id group by a.s_id, a.name order by mc;
That's pretty cool!
Reference (TIP 22) http://osdn.dl.sourceforge.net/sourc...ADME_mysql.txt
It's also possible to compute running aggregates. Assume the following
table
and data:
CREATE TABLE dice (
d_id int(11) NOT NULL auto_increment,
roll int,
PRIMARY KEY (d_id)
);
insert into dice (roll) values (1);
insert into dice (roll) values (2);
insert into dice (roll) values (3);
insert into dice (roll) values (4);
insert into dice (roll) values (5);
insert into dice (roll) values (6);
The following gives a running, cumulative, sum:
mysql> select a.roll, sum(b.roll) from dice a, dice b
where b.d_id <= a.d_id group by a.d_id, a.roll;
+------+-------------+
| roll | sum(b.roll) |
+------+-------------+
| 1 | 1 |
| 2 | 3 |
| 3 | 6 |
| 4 | 10 |
| 5 | 15 |
| 6 | 21 |
+------+-------------+
6 rows in set (0.00 sec)
To get a running average change the
sum to avg as follows:
mysql> select a.roll, avg(b.roll) from dice a, dice b
where b.d_id <= a.d_id group by a.d_id, a.roll;
+------+-------------+
| roll | avg(b.roll) |
+------+-------------+
| 1 | 1.0000 |
| 2 | 1.5000 |
| 3 | 2.0000 |
| 4 | 2.5000 |
| 5 | 3.0000 |
| 6 | 3.5000 |
+------+-------------+
6 rows in set (0.00 sec)
Regards,
Mike Chirico
Reference (TIP 23) http://osdn.dl.sourceforge.net/sourc...ADME_mysql.txt
"Siemel Naran" <Si*********@RE MOVE.att.net> wrote in message
news:VwCmc.2716 7 "Mike Chirico" <mc******@comca st.net> wrote in message select a.name,sum(1) as mc from student a, student b where a.s_id <= b.s_id group by a.s_id, a.name order by mc;
Reference (TIP 22) http://osdn.dl.sourceforge.net/sourc...ADME_mysql.txt
That's pretty cool!
Instead of sum(1) you could use count(*).
What's the running time of the algorithm? Because it selects from 2 tables
it seems a O(N^2). If the system uses the index on s_id it can eliminate
the inner loop and immediately find the records of interest -- for example,
if driving by alias 'a', it can immediately find the records in alias 'b'
meeting the condition in the where clause. But it still has to loop through
these records in order to determine the count(*) or sum(1), because this
informations is (probably) not available in the index. Which makes it
O(N^2) still. Of course, if N is small as in the examples in this thread,
then it doesn't matter. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Thomas Jespersen |
last post by:
Hi
I have a simple question:
How do I get the full url of the virtuel path of my website.
Eg. on my dev machine it is http://localhost:6786/website1 and on the
production server it will be http://www.mycompany.dk.
/Thomas
|
by: klimmer |
last post by:
Hello,
I'am used to work with PHP and MySQL on webbased application.
I have to make for my colleagues a program that have to follow a few
dessistions (path) like a flowchart.
The result shood be a word document from about, 150-250 pages. The
chapters are, depending of the anwsers in the virtuel flowchart.
Afterwoods there are more then 100 fields automaticaly filled in from
excel tabels. What qind of fields are depending of the flow...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |