Zend how do I create a left join - sql

How do I turn this left join query:
select advertisercontest.*, advertiseraccount.advertiserid, advertiseraccount.companyname
from advertisercontest
left join advertiseraccount on advertiseraccount.loginid = advertisercontest.loginid
where advertisercontest.golive is not NULL;
into a left join in Zend?

You could do as follows:
$db = Zend_Db_Table::getDefaultAdapter();
$select = $db->select();
$select->from('advertisercontest', '*')
->joinLeft(
'advertiseraccount',
'advertiseraccount.loginid = advertisercontest.loginid',
array('advertiseraccount.advertiserid', 'advertiseraccount.companyname')
)
->where('advertisercontest.golive is not NULL');;
$result = $db->fetchAll($select);
var_dump($result);
Here is the Zend_Db_Select documentation.

Related

Convert a $_GET value in PDO

I wrote a basic function to join 3 tables regarding a similar id.
{
$id = $_GET["id"];
$product_edit_query = $this->DB->query('
SELECT * FROM products
LEFT JOIN products_ingredients
ON products_ingredients.product_id = products.product_id
LEFT JOIN products_languages
ON products_languages.product_id = products.product_id
WHERE products.product_id = 73
');
$product_edit = $product_edit_query->fetch();
$this->smarty->assign('product_edit', $product_edit);
}
In the last line I have of my request, i have WHERE products.product_id = 73. 73 is hardcoded, and should be the value of $_GET["id"], but all I tried didn't work to convert it in PDO, any help ?
Solution given by a friend:
{
$stmt = $this->DB->prepare('
SELECT * FROM products
LEFT JOIN products_ingredients
ON products_ingredients.product_id = products.product_id
LEFT JOIN products_languages
ON products_languages.product_id = products.product_id
WHERE products.product_id = :id
');
$stmt->execute([':id' => $_GET["id"]]);
$get_product_base = $stmt->fetch();
$this->smarty->assign('get_product_base', $get_product_base);
}

DRUPAL db_or adding AND between multiple variables

I have inherited an application that is using the drupal filtering system to find results. The user can select to search the 'campus_location' column but when they enter multiple variables, it adds an AND between the condition.
Here is the printed version of the query generated by drupal:
SELECT * FROM champs c INNER JOIN users u ON u.uid = c.uid LEFT OUTER JOIN champs_rsvp r
ON r.uid = c.uid LEFT OUTER JOIN champs_checklists cl
ON cl.id = c.checklist_id LEFT OUTER JOIN node en
ON en.uid = u.uid AND en.type = 'getting_started_event'
LEFT OUTER JOIN node sn ON sn.uid = u.uid
AND sn.type = 'success_story'
LEFT OUTER JOIN champs_action_steps cas
ON cas.uid = u.uid
WHERE ( (c.campus_location LIKE 'Flint' ESCAPE '\\') )
**AND**( (c.campus_location LIKE 'Ann Arbor' ESCAPE '\\') )
I need the last AND to be an OR so it looks like this:
SELECT * FROM champs c INNER JOIN users u ON u.uid = c.uid LEFT OUTER JOIN champs_rsvp r
ON r.uid = c.uid LEFT OUTER JOIN champs_checklists cl
ON cl.id = c.checklist_id LEFT OUTER JOIN node en
ON en.uid = u.uid AND en.type = 'getting_started_event'
LEFT OUTER JOIN node sn ON sn.uid = u.uid
AND sn.type = 'success_story'
LEFT OUTER JOIN champs_action_steps cas
ON cas.uid = u.uid
WHERE ( (c.campus_location LIKE 'Flint' ESCAPE '\\') )
OR( (c.campus_location LIKE 'Ann Arbor' ESCAPE '\\') )
Which produces the correct results.
Here is the code that I inherited where it looks over the words in the search box. I just can't seem to figure out where it is adding the variables where it is putting the AND.
$words = explode(",", $_SESSION['form_state']['values']['filter']);
foreach($words as $word)
{
$or = db_or();
foreach($cols as $col)
{
$table = 'c';
if(in_array($col, array('completed_date', 'score')))
$table = 'cl';
elseif(in_array($col, array('name', 'mail')))
$table = 'u';
elseif($col == 'date')
$table = 'r';
elseif(in_array($col, array('announce', 'num_success_stories', 'num_action_steps'))) //can't put these in WHERE clause. Need to go in HAVING.
continue;
echo $word;
$or = $or->condition($table . '.' . $col, champs_make_like_sql($word), 'LIKE');
}
$result = $query->condition($or);
}

What will be the LINQ query for the below SQL Query

SELECT t_PersonalInformation.personalInformation_Name, t_PersonalInformation.personalInformation_PresentAddress,
t_Applicant.applicant_TotalExperience,
t_Experience.experience_CompanyName,
t_Experience.experience_Responsibilities,
t_Training.training_TitleDetails
FROM t_Applicant LEFT OUTER JOIN
t_PersonalInformation ON t_Applicant.applicant_user_ID = t_PersonalInformation.personalInformation_applicant_ID
LEFT OUTER JOIN
t_Experience ON t_Applicant.applicant_user_ID = t_Experience.experience_applicant_ID
LEFT OUTER JOIN
t_Training ON t_Applicant.applicant_user_ID = t_Training.training_applicant_ID
WHERE (t_Applicant.applicant_user_ID = 'hasib789')
I am working with C# with vs2008 for a asp.net application
It is depend on what mapping do you have.
For example it can be:
var result =
from a in DataContext.Applicant
join pi in DataContext.PersonalInformation on a.applicant_user_ID equals pi.personalInformation_applicant_ID
join e in DataContext.Experience on a.applicant_user_ID equals e.experience_applicant_ID
join t in DataContext.Training on a.applicant_user_ID equals t.training_applicant_ID
where a.applicant_user_ID == 'hasib789'
select new { personalInformation_Name = pi.personalInformation_Name, personalInformation_PresentAddress = pi.personalInformation_PresentAddress, applicant_TotalExperience = a.applicant_TotalExperience, experience_CompanyName = e.experience_CompanyName, experience_Responsibilities = e.experience_Responsibilities, training_TitleDetails = t.training_TitleDetails }

NHibernate how to create sql:on ID=s.PID AND p.Name = 'ABC'

[NUnit.Framework.Test]
public void Test2()
{
NHibernate.ISession session = Z.Core.NHibernateCore.NHibernateHelper.GetCurrentSession();
var crit = session.CreateCriteria("_School");
crit.CreateCriteria("_ListStudent", "__ListStudent", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
crit.Add(NHibernate.Criterion.Expression.Eq("__ListStudent.Name", "Abc"));
var list = crit.List();
Console.Write(list.Count);
}
NHibernate:
SELECT * FROM Tst_School this_ left outer join Tst_Student liststud1_ on this_.Guid=liststud1_.Guid WHERE liststud1_.Name = 'Abc'
How to create sql:
SELECT * FROM Tst_School this_ left outer join Tst_Student liststud1_ on this_.Guid=liststud1_.Guid AND liststud1_.Name = 'Abc'
Thanks
You should check WITH clause in HQL. I don't know if it's possible in CriteriaQuery.
http://nhforge.org/blogs/nhibernate/archive/2009/05/17/nhibernate-2-1-0-hql-with-clause.aspx

changing left join to basic join

I currently have some SQL that should return 3 rows of data but returns 6 (3 rows repeated twice).
I believe this is down to my syntax and want to try and build the query using basic joins, currently the SQL looks like this,
`function getMultiContentById($id) {
$query = "SELECT
FROM `mailers`
LEFT JOIN `mailer_content` ON `mailers`.`id` = `mailer_content`.`mailer_id`
LEFT JOIN `mailer_images` ON `mailer_content`.`id` = `mailer_images`.`content_id`
WHERE `mailers`.`id` = $id"
$result = runSelectArray($query, __FUNCTION__);
return $result;
}`
I want to use something like this
`WHERE `mailer_content`.id = `mailers.id`
Just change the LEFT to INNER on the first join, as in
$query = "SELECT
FROM `mailers`
INNER JOIN `mailer_content` ON `mailers`.`id` = `mailer_content`.`mailer_id`
LEFT JOIN `mailer_images` ON `mailer_content`.`id` = `mailer_images`.`content_id`
WHERE `mailers`.`id` = $id"
$result = runSelectArray($query, __FUNCTION__);
return $result;
Share and enjoy.