Simple Machines Community Forum

General Community => Scripting Help => Topic started by: Makavelli on August 08, 2003, 10:52:19 AM

Title: mySQL Query help
Post by: Makavelli on August 08, 2003, 10:52:19 AM
I'm trying to use this select statement but it doesn't seem to work.

------------------
SELECT *
FROM notifications
WHERE notifications.eventID =(SELECT events.eventID FROM events WHERE eventDate < CURRENT_DATE)
------------------

but i'm getting this error:

Error Code : 1064
You have an error in your SQL syntax near 'SELECT events.eventID FROM events WHERE eventDate < CURRENT_DATE)' at line 3

can someone please help?
Title: Re:mySQL Query help
Post by: Zef Hemel on August 08, 2003, 11:03:02 AM
MySQL does not support subselects for as far as I know.
Title: Re:mySQL Query help
Post by: Michele on August 08, 2003, 11:11:30 AM
Read up on INNER JOIN and/or LEFT JOIN, that will do what you want.
Title: Re:mySQL Query help
Post by: Spaceman-Spiff on August 08, 2003, 11:18:25 AM
yup, mysql doesnt support nested queries
you have to nest it in the php part
Title: Re:mySQL Query help
Post by: Makavelli on August 08, 2003, 11:35:13 AM
alright.. i'll do that. thnx a lot.. at least now i won't go crazy trying to figure it out..   ;D
Title: Re:mySQL Query help
Post by: Michele on August 08, 2003, 01:47:18 PM
Quote from: Makavelli on August 08, 2003, 10:52:19 AM
I'm trying to use this select statement but it doesn't seem to work.

------------------
SELECT *
FROM notifications
WHERE notifications.eventID =(SELECT events.eventID FROM events WHERE eventDate < CURRENT_DATE)
------------------

but i'm getting this error:

Error Code : 1064
You have an error in your SQL syntax near 'SELECT events.eventID FROM events WHERE eventDate < CURRENT_DATE)' at line 3

can someone please help?

SELECT not.*
FROM notifications as not
LEFT JOIN events
ON events.eventID =not.eventID
WHERE events.eventDate < CURRENT_DATE

Try that and let me know if it works!
Michele
Title: Re:mySQL Query help
Post by: Iridium on August 08, 2003, 07:16:51 PM
You may find it faster to use a JOIN rather than a LEFT JOIN as (from what I've seen) JOIN makes better use of any indices/keys that may be defined.
Title: Re:mySQL Query help
Post by: Gobalopper on August 08, 2003, 07:37:04 PM
I thought the latest version of MySQL added sub selects?
Title: Re:mySQL Query help
Post by: [Unknown] on August 08, 2003, 07:46:55 PM
Quote from: Gobalopper on August 08, 2003, 07:37:04 PM
I thought the latest version of MySQL added sub selects?

It did, but not like the one used above...

This is the faster way to do it (right join, instead of left join.)

SELECT n.*
FROM notifications as n, events AS e
WHERE e.eventDate < CURRENT_DATE()
   AND e.eventID = n.eventID

-[Unknown]
Title: Re:mySQL Query help
Post by: Spaceman-Spiff on August 08, 2003, 07:54:30 PM
ah yes, it's in mysql 4.1.x
didnt know that...
but not everyone uses that yet...