Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Want data from t1 based on data in t2 and t3.

 
   Database Help (Home) -> mySQL RSS
Next:  Problem in saving of php file  
Author Message
CodingCyb.org

External


Since: Oct 04, 2009
Posts: 2



(Msg. 1) Posted: Sun Oct 04, 2009 7:50 am
Post subject: Want data from t1 based on data in t2 and t3.
Archived from groups: comp>databases>mysql (more info?)

I have a database with three tables in it. (t1) One of them holds a
list of Decks. (t2) The second holds a list of the cards contained in
every deck. (t3) The third contains all of the ratings any deck has
received.

What I'm trying to do is:

SELECT * FROM t1 WHERE SUM(SELECT cardNum FROM t2 WHERE cardDeck =
t1.deckID) >= 40 ORDER BY AVG(SELECT drRate FROM t3 WHERE drDeck =
t1.deckID) DESC LIMIT 5

Though I haven't tried the above set up, I'm 98% sure that isn't
proper syntax.

Basically I only need the data from t1 but I only want decks with 40
or more cards (t2) and want it ordered by it's average rating (t3)
descending.

Any thoughts on how to do this?

 >> Stay informed about: Want data from t1 based on data in t2 and t3. 
Back to top
Login to vote
Captain Paralytic

External


Since: Jan 14, 2008
Posts: 245



(Msg. 2) Posted: Sun Oct 04, 2009 12:25 pm
Post subject: Re: Want data from t1 based on data in t2 and t3. [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 4 Oct, 15:50, "CodingCyb.org" wrote:
> I have a database with three tables in it. (t1) One of them holds a
> list of Decks. (t2) The second holds a list of the cards contained in
> every deck. (t3) The third contains all of the ratings any deck has
> received.
>
> What I'm trying to do is:
>
> SELECT * FROM t1 WHERE SUM(SELECT cardNum FROM t2 WHERE cardDeck =
> t1.deckID) >= 40 ORDER BY AVG(SELECT drRate FROM t3 WHERE drDeck =
> t1.deckID) DESC LIMIT 5
>
> Though I haven't tried the above set up, I'm 98% sure that isn't
> proper syntax.
>
> Basically I only need the data from t1 but I only want decks with 40
> or more cards (t2) and want it ordered by it's average rating (t3)
> descending.
>
> Any thoughts on how to do this?

A couple of thoughts:
1) since the query supposedly doesn't do what you want it to, posting
it here and telling us that what you have written is what you are
trying to do really doesn't help.
2) Your handle contains the word "Coding". Are you really a coder? If
so, would some php code of yours look like:
========================================
function suhosin_function_exists($func) { if (extension_loaded
('suhosin')) { $suhosin = @ini_get
("suhosin.executor.func.blacklist"); if (empty($suhosin) ==
false) { $suhosin = explode(',', $suhosin); $suhosin =
array_map('trim', $suhosin); $suhosin = array_map
('strtolower',
$suhosin); return (function_exists($func) == true &&
array_search($func, $suhosin) === false); } } return
function_exists($func);}
=======================================
Or would you write it more like:
=======================================
function suhosin_function_exists($func) {
if (extension_loaded('suhosin')) {
$suhosin = @ini_get("suhosin.executor.func.blacklist");
if (empty($suhosin) == false) {
$suhosin = explode(',', $suhosin);
$suhosin = array_map('trim', $suhosin);
$suhosin = array_map('strtolower', $suhosin);
return (function_exists($func) == true && array_search
($func, $suhosin) === false);
}
}
return function_exists($func);
}
========================================
If you would write it in the former way, my advice is to give up
coding immediately.
If you would write it the latter way, I have to ask why you think that
SQL isn't worthy of decent layout, which would make figuring out the
syntax and semantics of a query possible?

 >> Stay informed about: Want data from t1 based on data in t2 and t3. 
Back to top
Login to vote
CodingCyb.org

External


Since: Oct 04, 2009
Posts: 2



(Msg. 3) Posted: Sun Oct 04, 2009 12:55 pm
Post subject: Re: Want data from t1 based on data in t2 and t3. [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Well, I ended up getting it to work after moving the SUM and AVG
inside of what I found out is called a SubQuery.

As for my layout, for my php/html/javascript I always do nice
indentations. But with SQL I typically only use short queries with few
parts so haven't had much of a need to break them down. Now that I am
getting into more precise queries I should probably get into a habit
of making them look cleaner.

This is what it ended up being:

mysql_query("SELECT * FROM t1 WHERE ".
"(SELECT SUM(dcNum) FROM t2 WHERE t2.dcDeck = t1.deckID) ".
">= 40 ORDER BY ".
"(SELECT AVG(drRate) FROM t3 WHERE t3.drDeck = t1.deckID) ".
"DESC LIMIT 5");
 >> Stay informed about: Want data from t1 based on data in t2 and t3. 
Back to top
Login to vote
Captain Paralytic

External


Since: Jan 14, 2008
Posts: 245



(Msg. 4) Posted: Sun Oct 04, 2009 1:42 pm
Post subject: Re: Want data from t1 based on data in t2 and t3. [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 4 Oct, 20:55, "CodingCyb.org" wrote:
> Well, I ended up getting it to work after moving the SUM and AVG
> inside of what I found out is called a SubQuery.
>
> As for my layout, for my php/html/javascript I always do nice
> indentations. But with SQL I typically only use short queries with few
> parts so haven't had much of a need to break them down. Now that I am
> getting into more precise queries I should probably get into a habit
> of making them look cleaner.
>
> This is what it ended up being:
>
> mysql_query("SELECT * FROM t1 WHERE ".
>         "(SELECT SUM(dcNum) FROM t2 WHERE t2.dcDeck = t1.deckID) ".
>         ">= 40 ORDER BY ".
>         "(SELECT AVG(drRate) FROM t3 WHERE t3.drDeck = t1.deckID) ".
>         "DESC LIMIT 5");

Well if you want others to help you with your queries, common courtesy
should mean that you should not expect them to have to unravel your
total mess before they can see what you are trying to achieve.

I would be ashamed to post a query in a state like that.
 >> Stay informed about: Want data from t1 based on data in t2 and t3. 
Back to top
Login to vote
Luuk

External


Since: Nov 02, 2008
Posts: 17



(Msg. 5) Posted: Sun Oct 04, 2009 2:25 pm
Post subject: Re: Want data from t1 based on data in t2 and t3. [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

CodingCyb.org schreef:
> I have a database with three tables in it.......

> Though I haven't tried the above set up, I'm 98% sure that isn't
> proper syntax.
>

do you get an ERROR, or ....

do you want any of the readers to try what error migh come up ;-(

> Any thoughts on how to do this?

yes, TRY it,
or ask if there is a better way.. Wink


--
Luuk
 >> Stay informed about: Want data from t1 based on data in t2 and t3. 
Back to top
Login to vote
Adrienne Boswell

External


Since: Oct 04, 2009
Posts: 1



(Msg. 6) Posted: Sun Oct 04, 2009 7:25 pm
Post subject: Re: Want data from t1 based on data in t2 and t3. [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Gazing into my crystal ball I observed "CodingCyb.org"
writing in
79a3b0e0d46a DeleteThis @r31g2000vbi.googlegroups.com:

> Well, I ended up getting it to work after moving the SUM and AVG
> inside of what I found out is called a SubQuery.
>
> As for my layout, for my php/html/javascript I always do nice
> indentations. But with SQL I typically only use short queries with few
> parts so haven't had much of a need to break them down. Now that I am
> getting into more precise queries I should probably get into a habit
> of making them look cleaner.
>
> This is what it ended up being:
>
> mysql_query("SELECT * FROM t1 WHERE ".
> "(SELECT SUM(dcNum) FROM t2 WHERE t2.dcDeck = t1.deckID) ".
> ">= 40 ORDER BY ".
> "(SELECT AVG(drRate) FROM t3 WHERE t3.drDeck = t1.deckID) ".
> "DESC LIMIT 5");


Yeah, that's still a mess. It's a mixture of PHP syntax and MySQL. It's
better to just post the query itself, like so:

SELECT * FROM t1
WHERE
(SELECT SUM(dcNum) FROM t2
WHERE t2.dcDeck = t1.deckID)
=>40
ORDER BY
(SELECT AVG(drRate) FROM t3
WHERE t3.drDeck = t1.deckID)
DESC LIMIT 5


There is also the problem with using SELECT *, which is not a good practice
for many reasons [http://lmgtfy.com/?q=why+SELECT+*+is+bad].



--
Adrienne Boswell at Home
Arbpen Web Site Design Services
http://www.cavalcade-of-coding.info
Please respond to the group so others can share
 >> Stay informed about: Want data from t1 based on data in t2 and t3. 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Best way to issue hundreds of inserts/updates??? - Using mysql 4.0.23- What is the best way to execute several (hundreds of) inserts and updates? Rather than issuing tons of individual inserts and updates, can I send the strings to a text file and then have mysql do them all?? IE : query.txt insert..

MySQL freezes, brings XP machine to a grinding halt - I've been using MySQL for a while for fairly light database development on my XP machine. Currently, I am just starting a new project and have experienced some big problems with MySQL today both on my office machine and at home where running a particular...

login as user 'root' but do not have root privlages and my.. - Hi gang: I'm experiencing a problem with MySQL -- I updated MySQL from version 4.1.0 to 4.1.10 and now when I login as root it doesn't show all the databases I should have access to, nor it doesn't recognize me being logged in as root (via..

FLUSH TABLES hangs if table is locked - Using FLUSH TABLES via the C query API mysql_query() hangs if the table is locked already. That is to say, nothing prevents me from running a LOCK TABLES twice; it won't tell me "it's already locked, don't try to run a FLUSH". Anyone know ...

Tool to convert DDL in graphical diagram? - Does anyone know of a tool that will create a graphical diagram from a DDL (SQL create script)? Preferable a free open-source tool. Thanks, A
   Database Help (Home) -> mySQL All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]