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.

The Database Forum


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



Reply
Old 07-05-2008, 06:11 PM MySQL question
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
This is quite hard to explain, but Ill give it a shot.

I have a table, with 3 fields. One of the fields contains a numeric value [let's say it can either be 1, 2, 3 or 4 in this case].

I need to produce an SQL statement that will select another field [in this case; 'filename'], but only one for each of the numbers that I mentioned before.

e.g. lets say there are 2 records for '1', and 3 records for '4', I only want to retrieve the first record from '1' and the first record from '4'.

I think it may be possible using the
Code:
SELECT DISTINCT
command, but Im not too sure how??

Any help would be great!
__________________

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


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


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

Please login or register to view this content. Registration is FREE
pealo86 is online now
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
 
Register now for full access!
Old 07-06-2008, 04:46 AM Re: MySQL question
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,023
Name: Forrest Croce
Location: Seattle, WA
Trades: 0
Select Filename From Table Where Numeric = 1 Limit 1
Union All Select Filename From Table Where Numeric = 2 Limit 1
Union All Select Filename From Table Where Numeric = 3 Limit 1
Union All Select Filename From Table Where Numeric = 4 Limit 1
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Old 07-06-2008, 06:09 AM Re: MySQL question
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
Thanks. I should have mentioned however, that the numeric field could have a limitless amount of numbers, not necesarrily just up to 4.

Is there an SQL command that could compensate for this? or would it just be best to create a PHP loop to do it?
__________________

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


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


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

Please login or register to view this content. Registration is FREE
pealo86 is online now
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
Old 07-06-2008, 02:57 PM Re: MySQL question
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,023
Name: Forrest Croce
Location: Seattle, WA
Trades: 0
Can you write custom aggregates in mysql? How, exactly, are you defining the first match?
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Old 07-06-2008, 03:53 PM Re: MySQL question
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
Hmmm sorry I dont follow you?

Here is the SQL if that helps!
Code:
--
-- Table structure for table `tblportimg`
--

CREATE TABLE `tblportimg` (
  `portImgId` int(15) NOT NULL auto_increment,
  `portId` int(10) NOT NULL,
  `portImgFile` varchar(15) collate latin1_general_ci NOT NULL,
  PRIMARY KEY  (`portImgId`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=61 ;

--
-- Dumping data for table `tblportimg`
--

INSERT INTO `tblportimg` (`portImgId`, `portId`, `portImgFile`) VALUES
(49, 2, '26.jpg'),
(50, 2, '27.jpg'),
(51, 3, '28.jpg'),
(52, 1, '29.jpg');
Thanks.
__________________

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


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


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

Please login or register to view this content. Registration is FREE
pealo86 is online now
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
Old 07-08-2008, 01:02 PM Re: MySQL question
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
DISTINCT may not work for a single query as it applies across the field list so if you have the same filename with 2 different IDs one will be missed.

GROUP BY should work though

SELECT portId,portImgFile FROM tblportimg GROUP BY portId
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 07-08-2008, 06:49 PM Re: MySQL question
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
Quote:
Originally Posted by chrishirst View Post
DISTINCT may not work for a single query as it applies across the field list so if you have the same filename with 2 different IDs one will be missed.

GROUP BY should work though

SELECT portId,portImgFile FROM tblportimg GROUP BY portId
Wayyyy it worked! Thanks a lot
__________________

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


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


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

Please login or register to view this content. Registration is FREE
pealo86 is online now
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
Reply     « Reply to MySQL question
 

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.22387 seconds with 12 queries