If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > SQL-String problem

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-10-05, 07:21
Timeout Timeout is offline
Registered User
 
Join Date: Oct 2005
Posts: 3
SQL-String problem

Hi there,

i´ve got the following SQL-Problem: I am working on a picture gallery and wanted to display the last 6 photos uploaded but only 1 photo per user.

The table have:
--------------------
fotoid, userid, picturename, thumbname, category, dbtimestamp

but the String:
--------------------
SELECT fotoid, userid, picturename, thumbname, category, dbtimestamp FROM gallery WHERE category <> 99 GROUP BY fotoid,userid ORDER BY fotoid DESC LIMIT 0, 6
--------------------

wont work. it displays 2 or more photos from a user...

this one wont work too... gives me an error...

---------------------------------------
SELECT fotoid, userid, picturename, thumbname, category, dbtimestamp FROM gallery WHERE (((fotoid) In (SELECT Max(fotoid) AS Maxfromfotoid, userid FROM gallery GROUP BY userid))) ORDER BY dbtimestamp DESC LIMIT 0, 6
----------------------------------------

any ideas?

thanx for your help
Reply With Quote
  #2 (permalink)  
Old 10-11-05, 07:57
guelphdad guelphdad is offline
Registered User
 
Join Date: Mar 2004
Posts: 440
What error do you get for the second query? What version of MySQL are you on? If it is older than 4.1 you can't do subqueries.
Reply With Quote
  #3 (permalink)  
Old 10-11-05, 08:51
felixg felixg is offline
Registered User
 
Join Date: Apr 2005
Location: Lier, Belgium
Posts: 122
Quote:
Originally Posted by Timeout
this one wont work too... gives me an error...

---------------------------------------
SELECT fotoid, userid, picturename, thumbname, category, dbtimestamp FROM gallery WHERE (((fotoid) In (SELECT Max(fotoid) AS Maxfromfotoid, userid FROM gallery GROUP BY userid))) ORDER BY dbtimestamp DESC LIMIT 0, 6
----------------------------------------
What error?

--
felix
Reply With Quote
  #4 (permalink)  
Old 10-12-05, 04:21
Timeout Timeout is offline
Registered User
 
Join Date: Oct 2005
Posts: 3
The error is...

Quote:
mysql error: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT Max(fotoid) AS Maxfromfotoid, userid FROM gallery
mysql error number: 1064
mysql version: 4.0.15-nt
Reply With Quote
  #5 (permalink)  
Old 10-12-05, 04:34
felixg felixg is offline
Registered User
 
Join Date: Apr 2005
Location: Lier, Belgium
Posts: 122
Quote:
Originally Posted by Timeout
The error is...
mysql error: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT Max(fotoid) AS Maxfromfotoid, userid FROM gallery
mysql error number: 1064
mysql version: 4.0.15-nt
As guelphdad already said earlier: you can't do subqueries in your version of MySQL.

Perhaps http://dev.mysql.com/doc/refman/4.1/...group-row.html
can enlighten you.

--
felix
Reply With Quote
  #6 (permalink)  
Old 10-13-05, 10:09
Timeout Timeout is offline
Registered User
 
Join Date: Oct 2005
Posts: 3
Now i updated mySql and got this error...

Quote:
mysql error: Operand should contain 1 column(s)
mysql error number: 1241
mysql version: 4.1.14-nt
Reply With Quote
  #7 (permalink)  
Old 10-13-05, 10:17
felixg felixg is offline
Registered User
 
Join Date: Apr 2005
Location: Lier, Belgium
Posts: 122
Quote:
Originally Posted by Timeout
Now i updated mySql and got this error...
Replace
Code:
SELECT Max(fotoid) AS Maxfromfotoid, userid FROM gallery GROUP BY userid
with

Code:
SELECT Max(fotoid) AS Maxfromfotoid FROM gallery GROUP BY userid
--
felix
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On