473,320 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

mysql select query with unusual criteria fails

Here is the scope of what I need to do;

want:

enrollment_year
allowed (even if null)
all of ica

criteria:

1) join ica and i
2) ica.intern_id = 821
3) either (q.partner_name = 'ECSU' and joins with permissions
according to your intern_id AND ica.has_ecsu_essay = 'Y') OR
(ica.has_ecsu_essay != 'Y' AND there is NO row in
permissions with partner_id where q.partner_id = 'ECSU' according to
821)

This is what I've done so far and it fails:

SELECT distinct i.enrollment_year,
IF (p.intern_id = i.id AND p.intern_id = '821' AND
p.partner_name = q.partner_name AND upper(q.partner_name) = 'ECSU' AND
upper(ica.has_ecsu_essay) = 'Y'), '1', '0' AS allowed,
ica.*
FROM interns i, intern_complete_application ica, permissions p,
partners q
WHERE ica.intern_id = i.id
AND ica.intern_id = '821'
AND upper(ica.has_ref_letter_1) = 'Y'
AND upper(ica.has_ref_letter_2) = 'Y'
AND upper(ica.has_transcript) = 'Y'

I get syntax errors on this query, and every other combination gives
me syntax errors or wrong data. I have been trying for 2 days to get
this query and it's either that or I write this horrifically bloated
PHP method to do the same thing (it works but it takes about 15
seconds to execute the whole thing):

function isCompletedIntern($internID) { // BOOLEAN "METHOD"
global $dbHost,$dbPort,$dbUser,$dbPwd,$dbName;

$db = new dbConnection($dbHost,$dbPort,$dbUser,$dbPwd,$dbNam e);
$dbconnection = $db->connect();

$isCompleted = 1;

$query = 'SELECT * FROM intern_complete_application WHERE intern_id
= \'' . $internID . '\'';
$queryInfo = new mySqlQuery($query, $dbconnection);
$result = $queryInfo->getResult();
if (get_object_vars($result[0])) {
foreach (array('has_ref_letter_1', 'has_ref_letter_2',
'has_transcript', 'has_grad_enrollment', 'has_ecsu_essay') as $key =>
$val) {
$arrayKeyName = $this->dbNameToArrayName($val);
${$arrayKeyName} = $result[0]->$val;
}
$result = null;
}

$query = 'SELECT partner_id, upper(partner_name) as name FROM
partners ' .
'WHERE upper(partner_name) = \'ECSU\' OR upper(partner_name) =
\'GRAD\'';
$queryInfo =& new mySqlQuery($query, $dbconnection);
$result =& $queryInfo->getResult();

for ($i = 0; $i < sizeof($result); $i++) {
switch ($result[$i]->name) {
case 'ECSU':
$ecsuID = $result[$i]->partner_id;
break;
case 'GRAD':
$gradID = $result[$i]->partner_id;
break;
default:
// DO NOTHING
break;
}
}

// CHECK FOR ALL THREE REQUIRED CHECKBOXES TO MAP TO
intern_complete_application TO BE MARKED 'Y'
foreach(array('hasRefLetter1', 'hasRefLetter2', 'hasTranscript') as
$key => $val) {
if (strcmp(strtolower($$val), 'y') != 0) $isCompleted = 0;
}

/*----------------------------------------------------------------------------------------------------------
If they are an ECSU permitted intern and their required 1-page
essay is not yet marked 'checked'
in intern_complete_application they are not yet completed
-----------------------------------------------------------------------------------------------------------*/
if ($isCompleted) {
$query = 'SELECT allowed FROM permissions WHERE intern_id = \'' .
$internID . '\' ' .
' AND partner_id = \'' . $ecsuID . '\'';
$queryInfo =& new mySqlQuery($query, $dbconnection);
$result =& $queryInfo->getResult();
if (strcmp(strtolower($hasEcsuEssay), 'y') != 0 &&
get_object_vars($result[0])) {
if ($result[0]->allowed) $isCompleted = 0;
}
}
//---END OF ECSU REQUIREMENT
BLOCK--------------------------------------------------------------------------
/*----------------------------------------------------------------------------------------------------------
If they are a grad student (be sure to check in grads table as well
as permissions through LEFT JOIN
statements) and they have not submitted their proof-of-enrollment
form, that mark in intern_complete_
application will also be not marked 'Y' and thus they are not yet
completed
-----------------------------------------------------------------------------------------------------------*/
if ($isCompleted) {
$query = 'SELECT id FROM interns WHERE id = \'' . $internID . '\' '
..
' AND enrollment_year IN ' . $this->graduateCompletionFieldsSQL;
$queryInfo =& new mySqlQuery($query, $dbconnection);
$result =& $queryInfo->getResult();
if (strcmp(strtolower($hasGradEnrollment), 'y') != 0 &&
get_object_vars($result[0])) $isCompleted = 0;
}
//---END OF GRAD REQUIREMENT
BLOCK--------------------------------------------------------------------------

$db->close();
$result = null;
$queryInfo = null;
return $isCompleted;
}

I'm sorry to ask so many questions, guys. It's evident I am not a
good coder.

Phil
Jul 17 '05 #1
1 2731
[ not crossposted to alt.php ]

Phil Powell wrote:
[snip]
SELECT distinct i.enrollment_year,
IF (p.intern_id = i.id AND p.intern_id = '821' AND
p.partner_name = q.partner_name AND upper(q.partner_name) = 'ECSU' AND
upper(ica.has_ecsu_essay) = 'Y'), '1', '0' AS allowed,
ica.*
FROM interns i, intern_complete_application ica, permissions p,
partners q
WHERE ica.intern_id = i.id
AND ica.intern_id = '821'
AND upper(ica.has_ref_letter_1) = 'Y'
AND upper(ica.has_ref_letter_2) = 'Y'
AND upper(ica.has_transcript) = 'Y'


select if(<cond>, true, false) as whatever from wherever
works for me

select if(<cond>), true, false as whatever from wherever
doesn't
try:

SELECT ... IF (..._essay) = 'Y', '1', '0') AS allowed, ...

HTH

--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.
Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Benjamin Dickgießer | last post by:
Hi, I want to create a sql query but don't know if this is possible with mysql. The Query should do the following: Select all db entries from table in which entry a is smaller than the number...
0
by: Gordon | last post by:
I have 2 tables t and t1. In this case, t1 is a copy of t. I want to delete rows from t1 based on criteria on the t table and a relationship between t ad t1 (in this case the id column). In the...
0
by: harm | last post by:
Hi all, I think there is a bug in mysql 4: select week("2003-08-15") from po.orders limit 1; produces: 32 after:
51
by: w_curtis | last post by:
I'm an Access user, and I'm trying to learn MySQL and then PHP so I can make some web databases. But it just isn't clicking. I've followed some tutorials, and picked up a book, but just getting...
6
by: Ridge Burner | last post by:
Can someone tell me which of these 2 SQL queries will be more efficient? I'm having a debate with another guy about which would be less resource intensive for MySQL. The first uses MySQL to pick...
2
by: basestring | last post by:
Hi I am busy now for many hours without luck I have a database and when I use PHP to add date to it, it works only one time when i want to add the next data, It doesn't work. but i don't get any...
2
by: =?iso-8859-1?B?Sm/jbyBNb3JhaXM=?= | last post by:
Hi there guys, My doubt is related to PHP and MySQL usage, and it's related to check if performed queries are performed with success, since we know that: Each time we perform an sql query,...
8
by: The Natural Philosopher | last post by:
This is so weird. What I am trying to do is to upload files and stuff them in a mysql database. Everything works except the file content is zero. using the load_file command from mysql...
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.