473,246 Members | 1,194 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,246 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 2726
[ 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: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
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...

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.