This is a method that I do now.
I am just thinking about that program need to connect the extra table
'diploma'.
If the Mysql have a internal function can rank the ENUM in table Personnel
that will be wonderful.
----- Original Message -----
From: "Steve Howard" <[EMAIL PROTECTED]>
To: "About-tw.com ??????" <[EMAIL PROTECTED]>
Sent: Wednesday, July 04, 2001 7:44 AM
Subject: RE: Mysql DBI Select Syntax ?
Mornings are not a good time for me to answer questions...I have a little
more time this evening, and since SQL is really what I do, I think I can
give you an example of what I'm talking about and maybe you can do it in the
future. I am typing this directly into the e-mail here, so there may be a
syntax error somewhere, but I think I can get the idea across:
CREATE TABLE Diplomas (
diploma ENUM ("junior_high", "senior_high", "junior_college", "university",
"master", "doctor",
diploma_code int not null,
diploma_rank int not null)
CREATE TABLE Personnel (
FIRST_NAME varchar(20) not null,
MIDDLE_NAME varchar(20) null,
LAST_NAME varchar(20) not null,
SSN varchar(11) not null,
diploma_code int null)
INSERT INTO Diplomas (diploma, diploma_code, diploma_rank)
VALUES
("Junior High", 1,1)
INSERT INTO Diplomas (diploma, diploma_code, diploma_rank)
VALUES
("Senior High", 2,2)
INSERT INTO Diplomas (diploma, diploma_code, diploma_rank)
VALUES
("Junior College", 3,3)
INSERT INTO Diplomas (diploma, diploma_code, diploma_rank)
VALUES
("Master", 4,5)
INSERT INTO Diplomas (diploma, diploma_code, diploma_rank)
VALUES
("UNIVERSITY", 5,4)
INSERT INTO Diplomas (diploma, diploma_code, diploma_rank)
VALUES
("Doctorate", 6,6)
(At this point, you might want to select from your table to see what it
looks like. This is now a domain table giving valid diploma_codes for your
other tables. Here, you can go ahead and insert into the Personnel Table
several people with different diploma codes. Now, here's where you can now
do what you were saying, and I'll tell you when it comes to join syntax,
throw away the MySQL documentation, and what 95% of the MySQL people tell
you about how to do joins, and learn the ANSI join syntax. As you write
more, and more complex SQL you'll be very happy you have done this, and if
you ever need an outer join, you'll just be stuck if you try to do it using
the old SQL Syntax. I'll give you an example of old syntax, but first,
here's how to now do what you wanted to do using ANSI Join syntax);
SELECT l.FIRST_NAME,
case
when l.MIDDLE_NAME is null then ''
else l.MIDDLE_NAME
end as MIDDLE_NAME,
l.LAST_NAME,
case
when r.diploma is null then 'No Diploma'
else r.diploma
end as DIPLOMA
FROM Personnel l LEFT OUTER JOIN Diplomas r on l.diploma_code =
r.diploma_code
where r.diploma_rank > 3
ORDER BY r.diploma_rank
Now I used an outer join in this case even though it was not really
necessary. If you change your where clause to not restrict by diploma rank
using the outer join will return all the people, and if they have a diploma,
it will tell you which diploma they have. If you use an inner join, it will
only return people with diplomas.
Now, the syntax that you want to stay away from:
/*bad bad bad bad bad bad bad*/
SELECT l.FIRST_NAME,
case
when l.MIDDLE_NAME is null then ''
else l.MIDDLE_NAME
end as MIDDLE_NAME,
l.LAST_NAME,
case
when r.diploma is null then 'No Diploma'
else r.diploma
end as DIPLOMA
FROM Personnel l, Diplomas r
where r.diploma_rank > 3 and l.diploma_code = r.diploma_code
ORDER BY r.diploma_rank
/* end bad end bad end bad end bad end bac */
That is how 90 % of the MySQL people tell you to join a table, but I ask,
what if you want something besides a cross join, or an inner join....you'd
better be very good at writing multiple nested subqueries...and they'll be a
nightmare to debug, and read, and they'll be very inefficient. It's so much
better to just learn the ANSI join syntax.
One step further, if you're not sure what the diploma_code is for a certain
diploma, you can use a subquery like this:
SELECT l.FIRST_NAME,
case
when l.MIDDLE_NAME is null then ''
else l.MIDDLE_NAME
end as MIDDLE_NAME,
l.LAST_NAME,
case
when r.diploma is null then 'No Diploma'
else r.diploma
end as DIPLOMA
FROM Personnel l LEFT OUTER JOIN Diplomas r on l.diploma_code =
r.diploma_code
where r.diploma_rank > (SELECT diploma_rank from Diplomas
where diploma = "Junior College")
ORDER BY r.diploma_rank
Anyway, that's how to use a domain table in your definitions, and use it to
accomplish what you were asking. It just takes a little fooling with to get
the SQL down like you want it to be. Hopefully, I've given you enough
examples you can get a good start on design and programming with it.
Enjoy,
Steve Howard
-----Original Message-----
From: About-tw.com ?????? [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, July 03, 2001 9:19 AM
To: Steve Howard
Subject: Re: Mysql DBI Select Syntax ?
Anyway , Thanks a lot .
Sincerelly
Tom Wu
----- Original Message -----
From: "Steve Howard" <[EMAIL PROTECTED]>
To: "About-tw.com ??????" <[EMAIL PROTECTED]>
Cc: "DBI USERS" <[EMAIL PROTECTED]>
Sent: Tuesday, July 03, 2001 8:08 PM
Subject: RE: Mysql DBI Select Syntax ?
In this case, I really think the question is one of database design. In the
domain table you are creating to enumerate the types of degrees (a2), I
would add a "rank" column so I could evaluate on the rank column, and join
back to this domain table in queries where this evaluation needs to be made.
Once you have a numeric rank, see if you can then make your evaluation. As
it is, the rank is being determined by alphabetic order.
Steve H.
-----Original Message-----
From: About-tw.com ?????? [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, July 03, 2001 4:40 AM
To: Steve Howard; [EMAIL PROTECTED]
Subject: Re: Mysql DBI Select Syntax ?
Dear Steven,
Thanks for your opinion.
Here is my complete example ,
my $dbh = DBI->connect("DBI:mysql:xxxxx",,, { RaiseError => 1 });
my $sth = $dbh->prepare( qq{
CREATE TABLE a1 ( diploma
ENUM("junior_high","senior_high","junior_college","university","master","doc
tor")
});
$sth->execute;
$dbh->disconnect;
my $dbh = DBI->connect("DBI:mysql:xxxxx",,, { RaiseError => 1 });
my $sth = $dbh->prepare( qq{
CREATE TABLE a2 ( diploma
ENUM("junior_high","senior_high","junior_college","university","master","doc
tor")
});
$sth->execute;
$dbh->disconnect;
my $dbh = DBI->connect("DBI:mysql:xxxxx",,, { RaiseError => 1 });
my $sth =
repare( qq{
SELECT a1.diploma a2.diploma
FROM a1,a2
WHERE a1.diploma <= a2.diploma
});
$sth->execute;
$dbh->disconnect;
I'd like to compare a1.diploma and a2.diploma, and my ideal rule is doctor > master
> university > junior_college > .....
But the result is university > senior_high > junior_high > junior_college > master >
doctor
Is there any method let me get my ideal rule is doctor > master >
university > junior_college > senior_high > junior_high
Thanks in advance.
Sincerelly
Tom Wu
----- Original Message -----
From: "Steve Howard" <[EMAIL PROTECTED]>
To: "About-tw.com ??????" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>
Sent: Tuesday, July 03, 2001 10:59 AM
Subject: RE: Mysql DBI Select Syntax ?
I'm not completely sure I know what you're asking. If you're wanting to put
a numerical equivalent to the possible string values, in MySQL you can use a
CASE statement, like this:
SELECT case
WHEN diploma = 'junior_high' THEN 1
WHEN diploma = 'senior_high' THEN 2
WHEN diploma = 'junior_college' THEN 3
WHEN diploma = 'university' THEN 4
WHEN diploma = 'master' THEN 5
ELSE 6
END
AS DIPLOMA
FROM Tablename
You can embed some version of that to get a numerical return from a table
enumerated as you have said, however, it
still shouldn't return as you have put in your WHERE clause. You would still
have to use:
WHERE diploma = 'senior_high'
If you only wanted Senior
high grads.
Is this what you are asking?
Steve Howard
-----Original Message-----
From: About-tw.com ?????? [mailto:[EMAIL PROTECTED]]
Sent: Monday, July 02, 2001 3:16 PM
To: [EMAIL PROTECTED]
Subject: Mysql DBI Select Syntax ?
Dear All,
my $dbh = DBI->connect("DBI:mysql:xxxxx",,, { RaiseError => 1 });
my $sth = $dbh->prepare( qq{
CREATE TABLE $table_name (
diploma
ENUM("junior_high","senior_high","junior_college","university","master","doc
tor")
});
When I do the following procedure
my $dbh = DBI->connect("DBI:mysql:xxxxx",,, { RaiseError => 1 });
my $sth = $dbh->prepare( qq{ SELECT * FROM $table_name WHERE
diploma = 2 });
$sth->execute;
my $diploma = $sth -> fetchrow_array ;
$dbh->disconnect;
I can get $diploma = "senior_high"
Now here is my problem , How could I get the value of the $diploma = 2 ?
I'll really appreciated if someone can help me.