Using the SELECT operator 'AND' only if a variable is set - sql

What is the correct/efficient way to display results based on a variable if the variable is set? If variable is not set, the AND operator should not be used.
I apologize if this is a repeat, I clicked the suggested links and they did not make sense to me.
Near the end of code is my note with ^^^^^ marked.
For example:
$whatever = 123;
SELECT
DISTINCT terms.name as product_type_name,
tax.term_id as termidouter
FROM $wpdb->posts AS p
INNER JOIN wp_term_relationships r
ON p.ID = r.object_id
INNER JOIN wp_term_taxonomy tax
ON r.term_taxonomy_id = tax.term_taxonomy_id
INNER JOIN wp_terms terms
ON tax.term_id = terms.term_id
WHERE
tax.taxonomy = 'product_type'
AND p.post_status = 'publish'
AND p.post_type = 'product'
AND '$whatever ' = terms.term_id
^^^^ If $whatever is empty, I want to return results as if this line did not exist.
ORDER BY product_type_name
");
I was going to do an IF/ELSE but I figured that was the lazy way.
$whatever = 123;
if (empty($whatever)) {
// SELECT without the AND
} else {
// SELECT with the AND
}

You could do something like this:
AND CASE WHEN '$whatever ' IS NOT NULL THEN '$whatever ' ELSE terms.term_id END = terms.term_id

So... typically you'd want to use prepared statements, but if we're going this route, I would collect all my optional search criteria in an array as strings:
$myTerms=array();
if(!empty($whatever)) {
$myTerms[]="terms.term_id='" . $whatever . "'";
}
...
Then you can build your query easily like so:
$mySql = "SELECT * FROM whatever WHERE somefield='somevalue' ";
if(count($myTerms)>0) {
$mySql.=" AND " . implode(" AND ", $myTerms);
}
Please note that this is a basic example; you should also be checking any value you pass into your query for attacks etc.

Related

How to write join query with multiple column - LINQ

I have a situation where two tables should be joined with multiple columns with or condition. Here, I have a sample of sql query but i was not able to convert it into linq query.
select cm.* from Customer cm
inner join #temp tmp
on cm.CustomerCode = tmp.NewNLKNo or cm.OldAcNo = tmp.OldNLKNo
This is how i have write linq query
await (from cm in Context.CustomerMaster
join li in list.PortalCustomerDetailViewModel
on new { OldNLKNo = cm.OldAcNo, NewNLKNo = cm.CustomerCode } equals new { OldNLKNo = li.OldNLKNo, NewNLKNo = li.NewNLKNo }
select new CustomerInfoViewModel
{
CustomerId = cm.Id,
CustomerCode = cm.CustomerCode,
CustomerFullName = cm.CustomerFullName,
OldCustomerCode = cm.OldCustomerCode,
IsCorporateCustomer = cm.IsCorporateCustomer
}).ToListAsync();
But this query doesn't returns as expected. How do I convert this sql query into linq.
Thank you
You didn't tell if list.PortalCustomerDetailViewModel is some information in the database, or in your local process. It seems that this is in your local process, your query will have to transfer it to the database (maybe that is why it is Tmp in your SQL?)
Requirement: give me all properties of a CustomerMaster for all CustomerMasters where exists at least one PortalCustomerDetailViewModel where
customerMaster.CustomerCode == portalCustomerDetailViewModel.NewNLKNo
|| customerMaster.OldAcNo == portalCustomerDetailViewModel.OldNLKNo
You can't use a normal Join, because a Join works with an AND, you want to work with OR
What you could do, is Select all CustomerMasters where there is any PortalCustomerDetailViewModel that fulfills the provided OR:
I only transfer those properties of list.PortalCustomerDetailViewModel to the database that I need to use in the OR expression:
var checkProperties = list.PortalCustomerDetailViewModel
.Select(portalCustomerDetail => new
{
NewNlkNo = portalCustomerDetail.NewNlkNo,
OldNLKNo = portalCustomerDetail.OldNLKNo,
});
var result = dbContext.CustomerMasters.Where(customerMaster =>
checkProperties.Where(checkProperty =>
customerMaster.CustomerCode == checkProperty.NewNLKNo
|| customerMaster.OldAcNo == checkProperty.OldNLKNo)).Any()))
.Select(customerMaster => new CustomerInfoViewModel
{
Id = customerMaster.Id,
Name = customerMaster.Name,
...
});
In words: from each portalCustomerDetail in list.PortalCustomerDetailViewModel, extract the properties NewNKLNo and OldNLKNo.
Then from the table of CustomerMasters, keep only those customerMasters that have at least one portalCustomerDetail with the properties as described in the OR statement.
From every remaining CustomerMasters, create one new CustomerInfoViewModel containing properties ...
select cm.* from Customer cm
inner join #temp tmp
on cm.CustomerCode = tmp.NewNLKNo or cm.OldAcNo = tmp.OldNLKNo
You don't have to use the join syntax. Adding the predicates in a where clause could get the same result. Try to use the following code:
await (from cm in Context.CustomerMaster
from li in list.PortalCustomerDetailViewModel
where cm.CustomerCode == li.NewNLKNo || cm.OldAcNo = li.OldNLKNo
select new CustomerInfoViewModel
{
CustomerId = cm.Id,
CustomerCode = cm.CustomerCode,
CustomerFullName = cm.CustomerFullName,
OldCustomerCode = cm.OldCustomerCode,
IsCorporateCustomer = cm.IsCorporateCustomer
}).ToListAsync();
var result=_db.Customer
.groupjoin(_db.#temp ,jc=>jc.CustomerCode,c=> c.NewNLKNo,(jc,c)=>{jc,c=c.firstordefault()})
.groupjoin(_db.#temp ,jc2=>jc2.OldAcNo,c2=> c2.OldNLKNo,(jc2,c2)=>{jc2,c2=c2.firstordefault()})
.select(x=> new{
//as you want
}).distinct().tolist();

Extract the list of brands associated with a category in WooCommerce

In woocommerce, I have products which are classified in one (or more) categories (product_cat), and which have a brand attribute (pa_marque), and are only in one brand.
I'm trying to extract the list of brands associated with a category ...
By doing a SQL JOIN (this may not be the best solution) on the term_relationships, term_taxonomy, terms and posts tables I get something like this:
{"post_id":"23391","term_taxonomy_id":"1217","taxonomy":"product_cat","taxonomy_parent":"0"},
{"post_id":"23391","term_taxonomy_id":"1219","taxonomy":"product_cat","taxonomy_parent":"1217"},
{"post_id":"23391","term_taxonomy_id":"1943","taxonomy":"pa_marque","taxonomy_parent":"0"}
(i.e. product 23391 has 2 product_cat and 1 pa_marque ... but that's where I don't really know how to continue ...)
Would you have a clue?
I found Get WooCommerce product categories that contain a specific product brand answer code and I got it working making some little changes to the code as follow:
// Inspired by https://stackoverflow.com/a/61624358/1256770
function get_taxovalues_in_another_taxo_from_a_product_categories($taxonomy_tofind, $taxonomy_known, $cat_term_slug)
{
global $wpdb;
$results = $wpdb->get_results("
SELECT DISTINCT
t1.*
FROM {$wpdb->prefix}terms t1
INNER JOIN {$wpdb->prefix}term_taxonomy tt1
ON t1.term_id = tt1.term_id
INNER JOIN {$wpdb->prefix}term_relationships tr1
ON tt1.term_taxonomy_id = tr1.term_taxonomy_id
INNER JOIN {$wpdb->prefix}term_relationships tr2
ON tr1.object_id = tr2.object_id
INNER JOIN {$wpdb->prefix}term_taxonomy tt2
ON tr2.term_taxonomy_id = tt2.term_taxonomy_id
INNER JOIN {$wpdb->prefix}terms t2
ON tt2.term_id = t2.term_id
WHERE
tt1.taxonomy = '$taxonomy_tofind'
AND tt2.taxonomy = '$taxonomy_known'
AND t2.slug = '$cat_term_slug'
ORDER BY t1.name
");
$return = [];
if (!empty($results)) {
$term_names = [];
foreach ($results as $result) {
$term_link = get_term_link(get_term($result->term_id, $taxonomy_tofind), $taxonomy_tofind);
$term_names[] = '<a class="' . $result->slug . '" href="' . $term_link . '">'
. $result->name . '</a>';
}
$return = $term_names;
}
return $return;
}
// searching 'pa_marque' associated to product_cat "aiguilles-et-crochets"
$brands = get_taxovalues_in_another_taxo_from_a_product_categories('pa_marque', 'product_cat', 'aiguilles-et-crochets');
print(implode(" - ", $brands));
// searching 'pa_epaisseur-laine' associated to product_cat "laines"
$brands = get_taxovalues_in_another_taxo_from_a_product_categories('pa_epaisseur-laine', 'product_cat', 'laines');
print(implode(" - ", $brands));

DSL, and SQL query very slow

This question is both about Doctrine and Symfony2.
I've made a query using Doctrine DQL. And Doctrine generates an SQL like this;
SELECT f0_.id AS id0, f0_.nom AS nom1, f0_.prenom AS prenom2, f0_.email AS email3, p1_.move_distance AS move_distance4, a2_.adresse1 AS adresse15, a2_.adresse2 AS adresse26, p3_.nom AS nom7, v4_.nom AS nom8, v4_.url AS url9, v4_.cp AS cp10, v4_.insee AS insee11, v4_.lat AS lat12, v4_.lng AS lng13, COUNT(f0_.id) AS sclr17
FROM person_teacher p1_
INNER JOIN fos_user f0_ ON p1_.id = f0_.id
LEFT JOIN person_lesson p7_ ON f0_.id = p7_.person_id
LEFT JOIN lesson l6_ ON l6_.id = p7_.lesson_id AND (l6_.id = 1)
LEFT JOIN person_teacher_language p9_ ON p1_.id = p9_.personteacher_id
LEFT JOIN language l8_ ON l8_.id = p9_.language_id AND (l8_.id = 1)
LEFT JOIN note_value n10_ ON p1_.id = n10_.personTeacher_id
LEFT JOIN pays p3_ ON f0_.id_pays = p3_.id
LEFT JOIN note n5_ ON n10_.id_note = n5_.id
LEFT JOIN person_teacher_adresse p11_ ON p1_.id = p11_.personteacher_id
LEFT JOIN adresse a2_ ON a2_.id = p11_.adresse_id
LEFT JOIN ville v4_ ON a2_.id_ville = v4_.id
GROUP BY f0_.id LIMIT 2147483647 OFFSET 0;
The problem is about these joins:
LEFT JOIN person_lesson p7_ ON f0_.id = p7_.person_id
LEFT JOIN lesson l6_ ON l6_.id = p7_.lesson_id AND (l6_.id = 1)
LEFT JOIN person_teacher_language p9_ ON p1_.id = p9_.personteacher_id
LEFT JOIN language l8_ ON l8_.id = p9_.language_id AND (l8_.id = 1)
If I remove them, the request works. Long request, but works.
With the joins, the request is infinite (99.9% CPU time used by MySQL after 5mn) or maybe very long, but anyway, far too long.
How to optimize this query?
(PS : I thought the AND (l6_.id = 1) and the AND (l8_.id = 1) would act as "filters" and immediately remove unnecessary rows, but no, it makes things worse: it's faster if I remove those conditions, and add where clause at the end, something like: WHERE (l6_.id = 1) AND (l8_.id = 1))
Here's my DQL code:
$retour = $this->createQueryBuilder('p')
->select(array(
'p.id',
'p.nom',
'p.prenom',
'p.email',
'p.moveDistance',
'a.adresse1',
'a.adresse2',
'pn.nom as pays',
'v.nom AS ville_nom',
'v.url',
'v.cp',
'v.insee',
'v.lat',
'v.lng',
'ROUND(' .
$mul.' * ' .
'ACOS( ' .
'COS( RADIANS( '.$lat.')) * '.
'COS( RADIANS( v.lat )) * '.
'COS( RADIANS( v.lng )-radians('.$lng.')) + '.
'SIN( RADIANS( '.$lat.' )) * '.
'SIN( RADIANS( v.lat )) ' .
')'.
',2) AS distance',
($in_kilometers?'\'km\'':'\'miles\'').' AS unit',
'ROUND( AVG(n.importance), 1) AS importance',
'COUNT(p.id) AS total'
))
->leftJoin('p.noteValues', 'nv')
->leftJoin('p.paysNaissance', 'pn')
->leftJoin('nv.note', 'n')
->leftJoin('p.adresses', 'a')
->leftJoin('a.ville', 'v');
/* (!) Optimizer: find out why if I do a join "ON"
* it endlessly query. I did classical "join" then a "WHERE"
* at the end. Find out why this method is faster:
*/
if ($lesson_id>0) {
$retour = $retour
->leftJoin('p.lessons', 'le');
}
if ($language_id>0) {
$retour = $retour
->leftJoin('p.languages', 'ln');
}
if (($lesson_id>0) && ($language_id>0)) {
$retour = $retour
->where('le.id = :lesson_id')
->andWhere('ln.id = :language_id');
}
elseif ($lesson_id>0) {
$retour = $retour
->where('le.id = :lesson_id');
}
elseif ($language_id>0) {
$retour = $retour
->where('ln.id = :language_id');
}
$retour = $retour
->groupBy('p.id')
->having('distance>:dmin')
->andHaving('distance<=:dmax')
->addOrderBy($order_by_1, $order_sens_1)
->addOrderBy($order_by_2, $order_sens_2);
$params=array(
'dmin' => $distance_min,
'dmax' => $distance_max
);
if ($lesson_id>0) {
$params['lesson_id']= $lesson_id;
}
if ($language_id>0) {
$params['language_id']= $language_id;
}
$retour = $retour->setParameters($params);
$retour = $retour
->setFirstResult( $offset )
->setMaxResults( $limit );
return $retour;
I suggest to put the table with more rows after the FROM part of the select,
and change the line 'LEFT JOIN lesson l6_ ON l6_.id = p7_.lesson_id AND (l6_.id = 1)' this way : It seems that you don't need to join with p7 because you force to be l6_.id=1, so I change to this
'LEFT JOIN lesson l6_ ON (l6_.id = 1)'
Hope this help.
Without a look at your MySQL table definitions, to speed up your query make sure you have defined an index on each column involved in a join, because otherwise mysql must evaluate each record of the tables that miss the index.
Please add more details to your question and I'll edit the answer to reflect the changes.

Codeigniter: from sql to activerecord, what is the wrong?

I want to try change my sql code to active record, here is the original code and active record try. And the error message. Please tell me where is my wrong? Thanks.
SELECT `emlakilan`.`id`, `emlakdurum`.`durum`,`emlaktip`.`tip`,`semtler`.`semt`,`emlakilan`.`fiyat`,`emlakilan`.`tarih`,`resim`.`r1` FROM ucburcak.`emlakilan`
INNER JOIN `semtler` ON `emlakilan`.`semtId` = `semtler`.`semtid`
INNER JOIN `emlaktip` ON `emlakilan`.`emlakTipId` = `emlaktip`.`id`
INNER JOIN `emlakdurum` ON `emlakilan`.`emlakDurumId` =`emlakdurum`.`id`
LEFT JOIN `resim` ON `emlakilan`.`resimId` = `resim`.`id`
WHERE `emlakdurumId`=3 ORDER BY `emlakilan`.`id` DESC LIMIT 4;
//and here active record function
public function kucuk_ilan($durum ='3') // $durum:1=>tümü 2=>kiralık 3=>satılık 4=>takas
{
$query = $this->db->select('emlakilan.id, emlakdurum.durum, emlaktip.tip, semtler.semt, emlakilan.fiyat, emlakilan.tarih, resim.r1')
->from('emlakilan')
->join('semtler','emlakilan.semtId' === 'semtler.semtid','inner')
->join('emlaktip','emlakilan.emlakTipId'==='emlaktip.id','inner')
->join('emlakdurum','emlakilan.emlakDurumId'==='emlakdurum.id','inner')
->join('resim','emlakilan.resimId'==='resim.id','left')
->where('emlakdurumId'===$durum)
->order_by('emlakilan.id','desc')
->limit(4);
$query = $this->db->get('emlakilan');
return $query->result_array();
}
And, error message:
Error Number: 1066
Not unique table/alias: 'emlakilan'
SELECT emlakilan.id, emlakdurum.durum, emlaktip.tip, semtler.semt, emlakilan.fiyat, emlakilan.tarih, resim.r1 FROM (emlakilan, emlakilan) INNER JOIN semtler ON INNER JOIN emlaktip ON INNER JOIN emlakdurum ON LEFT JOIN resim ON WHERE 0 IS NULL ORDER BY emlakilan.id desc LIMIT 4
Filename: C:\Program Files\EasyPHP-12.1\www\3burcak\system\database\DB_driver.php
Line Number: 330
Thanks for help.
So 1. remove the 'from' line
2. the joins where wrong
try this:
public function kucuk_ilan($durum ='3') // $durum:1=>tümü 2=>kiralık 3=>satılık 4=>takas
{
$query = $this->db->select('emlakilan.id, emlakdurum.durum, emlaktip.tip, semtler.semt, emlakilan.fiyat, emlakilan.tarih, resim.r1')
->from('emlakilan')
->join('semtler','emlakilan.semtId = semtler.semtid','inner')
->join('emlaktip','emlakilan.emlakTipId = emlaktip.id','inner')
->join('emlakdurum','emlakilan.emlakDurumId = emlakdurum.id','inner')
->join('resim','emlakilan.resimId = resim.id','left')
->where('emlakdurumId', $durum)
->order_by('emlakilan.id','desc')
->limit(4);
$query = $this->db->get('emlakilan');
return $query->result_array();
}

LINQ to SQL - How to add a where clause to a left join?

This LINQ query expression emits a left join and works:
from p in Prices
join ip in ItemPrices
on new { p.PriceId, ItemId = 7 } equals
new { ip.PriceId, ip.ItemId }
into priceItemPrice
from pip in priceItemPrice.DefaultIfEmpty()
select new
{
pricesPriceId = p.PriceId,
z = (int?)pip.PriceId,
p.Content,
p.PriceMinQ
}
SQL emitted:
-- Region Parameters
DECLARE #p0 Int = 7
-- EndRegion
SELECT [t0].[priceId] AS [pricesPriceId],
[t1].[priceId] AS [z],
[t0].[price] AS [Content],
[t0].[priceMinQ] AS [PriceMinQ]
FROM [price] AS [t0]
LEFT OUTER JOIN [itemPrice] AS [t1]
ON ([t0].[priceId] = [t1].[priceId])
AND (#p0 = [t1].[itemId])
How can I get it to emit the SQL below? It just has the where clause tacked on the end. A where clause is not accepted under the "from pip" and a where lambda expression before DefaultIfEmpty() doesn't work. I know I can filter it out in the select, but that's not what I need.
SELECT [t0].[priceId] AS [pricesPriceId],
[t1].[priceId] AS [z],
[t0].[price] AS [Content],
[t0].[priceMinQ] AS [PriceMinQ]
FROM [price] AS [t0]
LEFT OUTER JOIN [itemPrice] AS [t1]
ON ([t0].[priceId] = [t1].[priceId])
AND (#p0 = [t1].[itemId])
WHERE [t1].[priceId] is null
Update
Oy vey, my mistake, the where clause did work - for some reason VS2008 was not behaving and giving me grief and my stomach was growling. I tested back in LinqPad and the where clause was fine. So this little addition did work:
...
from pip in priceItemPrice.DefaultIfEmpty()
*** where pip.ItemId == null ***
select new
...
Here is a sample of how OneDotNetWay has done something similar. I've tried to take their example and match up your query.
var query = p in Prices
join ip in ItemPrices
on
new { p.PriceId, ItemId = 7 }
equals
new { ip.PriceId, ip.ItemId }
into priceItemPrice
from pip in priceItemPrice.DefaultIfEmpty()
select new
{
pricesPriceId = p.PriceId,
z = (int?)pip.PriceId,
p.Content,
p.PriceMinQ
}