Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
mySQL Query : Can't do this?
Old 04-21-2006, 10:48 PM mySQL Query : Can't do this?
Skilled Talker

Posts: 69
Trades: 0
I'm trying to get the latest post from any entry in one of two tables in a database. The query I'm using is:

$result = mysql_query("SELECT * FROM `strategyguides`,`editorials` ORDER BY `date` DESC LIMIT 0 , 1");

It comes back with an error that says the 'date' section is "ambiguous"...

Any help?
Reality15 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-21-2006, 10:59 PM Re: mySQL Query : Can't do this?
mad_willsy's Avatar
Super Spam Talker

Latest Blog Post:
R&R Catering Hire Testimonial
Posts: 805
Name: Will Craig
Location: Cheltenham, Gloucestershire, UK
Trades: 0
I have had the same problem. I searched google and found it is because you are limiting to 1 record, 1 record cannot be sorted. I know what you want to do (arrange, then get first record) but this doesn't seem to be the way to do it.

Any suggestions?
__________________
Wont :P

Please login or register to view this content. Registration is FREE
mad_willsy is offline
Reply With Quote
View Public Profile Visit mad_willsy's homepage!
 
Old 04-22-2006, 04:01 AM Re: mySQL Query : Can't do this?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
it also could mean that a field called "date" appears in both tables

Also note that date is a reserved word and should not be used for column names which may also be the cause of the ambiguity error.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 04-22-2006, 05:40 AM Re: mySQL Query : Can't do this?
Experienced Talker

Posts: 40
Location: Denmark
Trades: 0
As chrishirst already writes, the date column is probbaly in both tables. You need to tell MySQL which of the two date you want (`strategyguides` or `editorials`). This is simply done by prefixing the tablename before the column name.. e.g: `editorials`.`date` or
`strategyguides`.`date` depending on which one you want.

So try

$result = mysql_query("SELECT * FROM `strategyguides`,`editorials` ORDER BY `editorials`.`date` DESC LIMIT 0 , 1");

or

$result = mysql_query("SELECT * FROM `strategyguides`,`editorials` ORDER BY `strategyguides`.`date` DESC LIMIT 0 , 1");

This is my first post - so go easy
dennismp is offline
Reply With Quote
View Public Profile Visit dennismp's homepage!
 
Old 04-22-2006, 06:34 AM Re: mySQL Query : Can't do this?
Skilled Talker

Posts: 69
Trades: 0
But I want it from both... I want the query to pick up the newest entry from two tables.
Reality15 is offline
Reply With Quote
View Public Profile
 
Old 04-22-2006, 06:54 AM Re: mySQL Query : Can't do this?
Experienced Talker

Posts: 40
Location: Denmark
Trades: 0
Make one for each...

$result = mysql_query("SELECT * FROM `strategyguides`,`editorials` ORDER BY `editorials`.`date` DESC, `strategyguides`.`date` DESC LIMIT 0 , 1");
dennismp is offline
Reply With Quote
View Public Profile Visit dennismp's homepage!
 
Old 04-22-2006, 08:03 AM Re: mySQL Query : Can't do this?
pushedtomb's Avatar
Super Talker

Posts: 100
Name: Nathen
Location: Casnewydd, De Cymru, UK
Trades: 0
Quote:
Originally Posted by chrishirst

Also note that date is a reserved word and should not be used for column names which may also be the cause of the ambiguity error.
As chrishirst says, date is reserved in mysql.. try changing the column names to something like

"$result = mysql_query("SELECT * FROM `strategyguides`,`editorials` ORDER BY `editorials`.`date1` DESC, `strategyguides`.`date1` DESC LIMIT 0 , 1");"
__________________

Please login or register to view this content. Registration is FREE


Quote:
- "I've often wondered how MS can say that other software "isn't compatible" with MS when MS stuff isn't even compatible with other MS stuff" - quote from Daily Thing.com
pushedtomb is offline
Reply With Quote
View Public Profile
 
Old 04-22-2006, 08:07 AM Re: mySQL Query : Can't do this?
mad_willsy's Avatar
Super Spam Talker

Latest Blog Post:
R&R Catering Hire Testimonial
Posts: 805
Name: Will Craig
Location: Cheltenham, Gloucestershire, UK
Trades: 0
What other words are reserved? for example is string reserved?
__________________
Wont :P

Please login or register to view this content. Registration is FREE
mad_willsy is offline
Reply With Quote
View Public Profile Visit mad_willsy's homepage!
 
Old 04-22-2006, 08:12 AM Re: mySQL Query : Can't do this?
Experienced Talker

Posts: 40
Location: Denmark
Trades: 0
http://dev.mysql.com/doc/refman/5.0/...ved-words.html lists reserved-words.. But if you use back-ticks (`) you should be safe.
dennismp is offline
Reply With Quote
View Public Profile Visit dennismp's homepage!
 
Old 04-22-2006, 06:17 PM Re: mySQL Query : Can't do this?
Skilled Talker

Posts: 69
Trades: 0
Alright, my next problem in what I figure will be a LONG line of mySQL query problems... lol

My example database is like this
ENTRY 1 : Bob, The Zoo, 3
ENTRY 2 : Frank, The Zoo, 4
ENTRY 3 : Tom, The Park, 5

What I want is a query that will find the one in the middle (called category), and list them out, but not puting any duplicates. For example, if it was to use the above database I gave then the output would be:
The Zoo
The Park

Is there any way to do this or no?
Reality15 is offline
Reply With Quote
View Public Profile
 
Old 04-23-2006, 04:51 AM Re: mySQL Query : Can't do this?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
use ... GROUP BY fieldname ... in the query

http://www.w3schools.com/sql/sql_groupby.asp
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 04-23-2006, 08:30 AM Re: mySQL Query : Can't do this?
Skilled Talker

Posts: 69
Trades: 0
Alright, and so to finish off this script, I need to add to that SQL query something that will let me know how many entries there are with the condition CATEGORY = THIS_CATEGORY

Here's the query right now: SELECT a.*, b.realname FROM `sounds` AS a LEFT JOIN `staff` AS b ON b.id = a.staffid GROUP BY `category` ORDER BY `id` ASC

Thank you for the continuous support and help!
Reality15 is offline
Reply With Quote
View Public Profile
 
Old 04-23-2006, 09:27 AM Re: mySQL Query : Can't do this?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
select count(*) from table as alias where column = criteria
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 04-23-2006, 02:34 PM Re: mySQL Query : Can't do this?
Skilled Talker

Posts: 69
Trades: 0
So I put these many many many different querys together to get the one final query, and it gave me an error. I don't know what's wrong with it, but maybe someone does.

SELECT a.*, b.realname,a.count(*) FROM `sounds` AS a LEFT JOIN `staff` AS b ON b.id = a.staffid GROUP BY `category` WHERE `category` = CONVERT(_utf8 'Front Gate' USING latin1) COLLATE latin1_general_ci ORDER BY `id` ASC

Thanks!
Reality15 is offline
Reply With Quote
View Public Profile
 
Old 04-23-2006, 03:34 PM Re: mySQL Query : Can't do this?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
and the error was ??
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 04-23-2006, 03:35 PM Re: mySQL Query : Can't do this?
Skilled Talker

Posts: 69
Trades: 0
#1064 - 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 '( * ) FROM `sounds` AS a LEFT JOIN `staff` AS b ON b . id = a . staffid GROUP BY' at line 1
Reality15 is offline
Reply With Quote
View Public Profile
 
Old 04-23-2006, 03:54 PM Re: mySQL Query : Can't do this?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
a.count(*) AS alias
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 04-23-2006, 04:11 PM Re: mySQL Query : Can't do this?
Skilled Talker

Posts: 69
Trades: 0
SELECT a.*, b.realname,a.count(*) AS `music` FROM `sounds` AS a LEFT JOIN `staff` AS b ON b.id = a.staffid GROUP BY `category` WHERE `category` = CONVERT(_utf8 'Front Gate' USING latin1) COLLATE latin1_general_ci ORDER BY `id` ASC

is what you're telling me, this is the error it gives me now:

#1064 - 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 '( * ) AS `music` FROM `sounds` AS a LEFT JOIN `staff` AS b ON b . id = a . staff' at line 1

Last edited by Reality15; 04-23-2006 at 04:13 PM..
Reality15 is offline
Reply With Quote
View Public Profile
 
Old 04-25-2006, 06:53 AM Re: mySQL Query : Can't do this?
Skilled Talker

Posts: 69
Trades: 0
Any last ideas before I give up on this completely?
Reality15 is offline
Reply With Quote
View Public Profile
 
Old 04-25-2006, 11:03 AM Re: mySQL Query : Can't do this?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
works ok for me (with different tables of course)

what version of MySQL is it? and is that a direct copy of the query?
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to mySQL Query : Can't do this?

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.53930 seconds with 12 queries