473,386 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

subselect question... shouldn't this work?


I am new to subselects, and what I really want is to do them in an update,
but the following shows a simpler select, that also doesn't seem to work as I
think it should. Advice? Do I just misunderstand how subselects are actually
executed?

(This is on mysql version 4.1.0-alpha-max-nt.)

First, here's the tables in question:

mysql> describe m;
+-------+---------+-----------+------+-----+---------+-------+
| Field | Type | Collation | Null | Key | Default | Extra |
+-------+---------+-----------+------+-----+---------+-------+
| toid | int(11) | binary | YES | | NULL | |
| rd | int(11) | binary | YES | | NULL | |
+-------+---------+-----------+------+-----+---------+-------+

mysql> describe p;
+---------+---------+-----------+------+-----+---------+-------+
| Field | Type | Collation | Null | Key | Default | Extra |
+---------+---------+-----------+------+-----+---------+-------+
| userid | int(11) | binary | | PRI | 0 | |
| pmnew | int(11) | binary | YES | | NULL | |
| pmtotal | int(11) | binary | YES | | NULL | |
+---------+---------+-----------+------+-----+---------+-------+

and the data in the tables...

mysql> select * from p;
+--------+-------+---------+
| userid | pmnew | pmtotal |
+--------+-------+---------+
| 1 | 0 | 0 |
| 2 | 0 | 0 |
+--------+-------+---------+
2 rows in set (0.00 sec)

mysql> select * from m;
+------+-------+
| toid | rd |
+------+-------+
| 1 | 0 |
| 1 | 0 |
| 1 | 0 |
| 1 | 12 |
| 1 | 15 |
| 1 | 123 |
| 1 | 12312 |
| 1 | 12312 |
| 1 | 123 |
| 2 | 0 |
| 2 | 0 |
| 2 | 1 |
| 2 | 2 |
+------+-------+
13 rows in set (0.00 sec)

mysql> select userid,pmtotal,pmnew, (select count(rd) from m where
toid=p.userid) calc_total, (select count(rd) from m where rd=0 and
toid=p.userid) calc_new from p where userid in (select distinct toid from m);
+--------+---------+-------+------------+----------+
| userid | pmtotal | pmnew | calc_total | calc_new |
+--------+---------+-------+------------+----------+
| 1 | 0 | 0 | 9 | 3 |
| 2 | 0 | 0 | NULL | NULL |
+--------+---------+-------+------------+----------+
Now, the first row has what I want and expect, in calc_total and calc_new...
but the second row doesn't. Why? Shouldn't the subselects in the field
selector part (not the where part) be "re-executed" for each value in the
IN()? If so, I'd expect calc_total to be '4', and calc_new to be '2' for the
second row. For example, if I manually fudge the WHERE ... IN, I get:

mysql> select userid,pmtotal,pmnew, (select count(rd) from m where
toid=p.userid) calc_total, (select count(rd) from m where rd=0 and
toid=p.userid) calc_new from p where userid in (2);
+--------+---------+-------+------------+----------+
| userid | pmtotal | pmnew | calc_total | calc_new |
+--------+---------+-------+------------+----------+
| 2 | 0 | 0 | 4 | 2 |
+--------+---------+-------+------------+----------+

which is exactly what I want, but all at once :-)

Ideas? Misunderstanding on my part? Bug?

(By the way, what I eventually want to do is an update to set pmtotal and
pmnew to be the calc_total and calc_new; in the real schema this is a
simplified version of, they are essentially 'caches' of the new/total
counts...)

Any comments appreciated.

Ben Margolin
=====
[ Ben Margolin -- be*********@yahoo.com -- be*@prince.org ]

__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com

--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/my***********...ie.nctu.edu.tw

Jul 19 '05 #1
0 2938

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Greg Stark | last post by:
So I have a query in which some of the select values are subqueries. The subqueries are aggregates so I don't want to turn this into a join, it would become too complex and postgres would have...
3
by: Neil Zanella | last post by:
Hello, I would like to ask the about the following... PostgreSQL allows tables resulting from subselects to be renamed with an optional AS keyword whereas Oracle 9 will report an error...
4
by: James | last post by:
I have a performance problem with the following query and variations on the subselect. The EXISTS version of the first example will complete in ~10 minutes. The NOT logic in both the examples...
4
by: Chris Kratz | last post by:
Hello all, We have run into what appears to be a problem with rules and subselects in postgres 7.4.1. We have boiled it down to the following test case. If anyone has any thoughts as to why...
6
by: Alex P | last post by:
Hi, when creating a query with a subselect SELECT name, (SELECT max(pop) FROM cities WHERE cities.state = states.name) AS max_pop FROM states; then it is not possible to sort after max_pop...
2
by: Morten K. Poulsen | last post by:
(re-post) Dear list, Please let me know if this is not the list to ask this kind of question. I am trying to optimize a query that joins two relatively large (750000 rows in each) tables....
6
by: Sebastien | last post by:
I have the following statement which I run successfully in... 1 hour 10 minutes. SELECT a.tsgicd as ACCT_ID, a.tsa5cd as SEC_ID, CASE WHEN (SUBSTRING(a.tsgicd, 6, 1) = 'R' or...
0
by: rfrenken | last post by:
I am trying to write a database query in an old version of Crystal Reports (7.0) that finds all of the accounts with multiple late fees and then prints out the details of those lines. I have the SQL...
4
by: johnfaulkner | last post by:
Hi, I am trying to perform a single select of data from 2 tables, table A and table B. Table B may have none, one or many corresponding rows. If table B has no corresponding rows then table B...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
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,...
0
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...
0
Oralloy
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.