Table1(identity1, a1,b1)
Table2(identity, foreign(identity1),foreign(identity1))
Now when I use this query
$query = $qb->select('a1', ' b1')
->from('table2', 'm1')
->join('ApiMapBundle:tabl1, 'u1', 'WITH', $qb->expr()->orX('m1. foreign(a1) =u1.identity', 'm1. foreign(b1) = u1.identity '))
->andWhere('m1.identity=:tt')
->setParameter('tt', $cn)
->getQuery()
->getResult();
Now the problem with this query is that sometimes it gives id let's tt:5 so it gives me a value like this
Array(
0 => Array(id => 1, b1=> 8000225),
1 => Array(id => 9, b1 => 8000234)) given).
Basically in table the values structure is like this
Table2(Identity=5,foreign1=9,foreign2=1)
Any idea that how can I exactly get the given structure? Because in some cases it is fine they give me proper foreign 1 and foreign 2 but in other cases it make it alternative. Any idea?
Related
I'm using the query below:
use Doctrine\ORM\Query\Expr\Join;
$query = $this->createQueryBuilder('ad')
->select('ad.id, ad.title, ad.year, ad.hours, ad.status')
->addSelect('rem.remark')
->leftJoin('ad.remark', 'rem', Join::WITH, "rem.language = 'NL'")
->getQuery()
->getResult();
This query is working fine and returns the remark of a ad in the Dutch language. The ad has a one-to-many relation with its remark.
Only I also have ads that have for example an English remark and not a Dutch one. The I will like to get the English remark of that one and on the others in the list still the Dutch remark. So too summarise making a priority list on the languages that are returned?
One way to solve this is to use an extra join without relation:
$query = $this->createQueryBuilder('ad')
->select('ad.id, ad.title, ad.year, ad.hours, ad.status')
->addSelect('rem.remark')
->leftJoin('ad.remark', 'rem', Join::WITH, "rem.language = 'NL' OR rem.language = 'EN'")
->leftJoin(Remark::class, 'customRem', Join::WITH,
"rem.id <> customRem.id
AND rem.ad = customRem.ad
AND customRem.language = 'NL'")
->where('customRem.id IS NULL')
->getQuery()
->getResult();
The idea is
if NL language remark exists for an ad, add this remark joined to each result row of this ad expect to itself(will be null)
if NL language remark does not exist and an EN exists, then the joined row will be null
Finally, the condition customRem.id IS NULL makes this work.
Multiple languages solution
In the case of 3 supported languages, because DE > EN > NL, you could do:
->leftJoin(Remark::class, 'customRem', Join::WITH,
"rem.id <> customRem.id AND rem.ad =
customRem.ad AND rem.language < customRem.language")
For multiple languages and suppose a "customized" ability to order the languages, you could use:
"rem.id <> customRem.id
AND rem.ad = customRem.ad AND
(case when rem.language = 'NL' THEN 3 " .
"when rem.language = 'EN' THEN 2 " .
"when rem.language = 'DE' THEN 1 ELSE 0 END) < (case when customRem.language = 'NL' THEN 3 " .
"when customRem.language = 'EN' THEN 2 " .
"when customRem.language = 'DE' THEN 1 ELSE 0 END)"
Alternatively, you could create "lang_position" table(id, language, position) and join twice to get the position from the language.
i have this query on drupal 6
$catq=' ( SELECT term_node.nid as node_id FROM {term_node} WHERE tid='.$catint.') as cat, '
i have upgrade it like this
$query=db_select('term_node');
->addfield('term_node', 'nid', 'node');
->field('term_node', 'node' );
->condition('term_node.tid', = , $catint);
$cat=$query->addfield($query, 'cat');
is it wrong?
You are doing it wrong.
The right syntax for query - keeping your data will be:
$query = db_select('term_node', 't')
->fields('t')
->condition('term_node.tid', $catint, '=')
->execute();
$logo = $query->fetchAll();
But in drupal 7 there is no table term_node, probably you need 'taxonomy_index'.
I'm using Symfony/Doctrine.
I'm trying to select last 4 rows from table, but im getting error.
$em = $this->getDoctrine()->getEntityManager();
$query = $em->createQuery(
'SELECT c FROM DprocMainBundle:Courses c ORDER BY id DESC LIMIT 4'
);
$course = $query->getResult();
This is my query but it shows error.
Expected end of string, got 'LIMIT'
How should i use limit, and get the LAST 4 rows?
thanks!
Use setMaxResults() to limit the number of results.
$course = $query->setMaxResults(4)->getResult();
If you want to use this for pagination you can add a setFirstResult() call.
$course = $query->setMaxResults(4)->setFirstResult(10)->getResult();
Ok i have this code:
SELECT
IFNULL(s2.id,s1.id) AS effectiveID,
IFNULL(s2.status, s1.status) AS effectiveStatus,
IFNULL(s2.user_id, s1.user_id) as effectiveUser,
IFNULL(s2.likes_count, s1.likes_count) as effectiveLikesCount
FROM statuses AS s1
LEFT JOIN statuses AS s2 ON s2.id = s1.shared_from_id
WHERE s1.user_id = 4310
ORDER BY effectiveID DESC
LIMIT 15
And i need to rewrite it to querybuilder. Something like that?
$fields = array('IFNULL(s2.id,s1.id) AS effectiveID','IFNULL(s2.status, s1.status) AS effectiveStatus', 'IFNULL(s2.user_id, s1.user_id) as effectiveUser','IFNULL(s2.likes_count, s1.likes_count) as effectiveLikesCount');
$qb=$this->_em->createQueryBuilder()
->select($fields)
->from('WallBundle:Status','s1')
->addSelect('u')
->where('s1.user = :user')
->andWhere('s1.admin_status = false')
->andWhere('s1.typ_statusu != :group')
->setParameter('user', $user)
->setParameter('group', 'group')
->leftJoin('WallBundle:Status','s2', 'WITH', 's2.id=s1.shared_from_id')
->innerJoin('s1.user', 'u')
->orderBy('s1.time', 'DESC')
->setMaxResults(15);
var_dump($query=$qb->getQuery()->getResult());die();
This error is
[Syntax Error] line 0, col 7: Error: Expected known function, got 'IFNULL'
Use COALESCE instead of IFNULL like this
$fields = array('COALESCE(s2.id,s1.id) AS effectiveID','COALESCE(s2.status, s1.status) AS effectiveStatus', 'COALESCE(s2.user_id, s1.user_id) as effectiveUser','COALESCE(s2.likes_count, s1.likes_count) as effectiveLikesCount');
COALESCE return the first value not null in the list, so if A is null and B not null, then COALESCE(A,B) will return B.
There is a Doctrine extension that adds this among others.
This is the DQL file from IFNULL.
https://github.com/beberlei/DoctrineExtensions/blob/master/src/Query/Mysql/IfNull.php
This chapter explains how you use them.
http://symfony.com/doc/2.0/cookbook/doctrine/custom_dql_functions.html
I'm hoping someone can help with a problem I can't wrap my limited knowledge around. I have two SQL queries like:
SELECT
SUM(Matrix.[ITEM1]) AS [ITEM1],
SUM(Matrix.[ITEM2]) AS [ITEM2]
FROM StoreList
INNER JOIN Matrix ON StoreList.[Store Number] = Matrix.[Store Number]
WHERE (StoreList.Region = '#Value')
SELECT
ITEMTYPES.type_description,
ITEMS.Quantity
FROM ITEMS
INNER JOIN ITEMTYPES ON ITEMS.type_id = ITEMTYPES.id
Now, what these do is return a list of values that I want to multiply together (for example, to get a final quantity for [ITEM1], multiply [ITEM1] by [Items.Quantity]. However the ITEMTYPES table holds a list of descriptions of ITEM (and primary key id), which is used for entering new items. The STORELIST table uses the descriptors in ITEMTYPES as column names(ie. The ItemTypes table has records called [Item1], [Item2]; the Column names in Matrix are also [Item1], [Item2]).
The question is this - from my limited experience (and I do mean limited - < 6months with SQL) there is no way to perform a calculation based upon column names - I would have to abstract out the totals in some way to another table, with additional fields for Region (this explains the sum() statements in query 1) and perform calculations that way - or does some have a more elegant idea?
Table Structures as per Laurence's request:
For Table [MATRIX]:
[Store Number] (PK, int, not null)
[ITEM1] (int, default 0)
[ITEM2] (int, default 0)
For Table [ITEMTYPES]:
[id] (PK, int, not null),
[ITEMTYPE] (nvarchar(50), not null)
Now, Example data (excuse any formatting issues!):
[MATRIX]
Store Number ITEM1 ITEM2
------------------------------
123456 1 15
678920 31 9
[ITEMTYPE]
id ITEMTYPE
------------------------------
1 ITEM1
2 ITEM2
Appreciate the response!
Ok so you have 2 results returned from these 2 queries into 2 different ordered pair lists or arrays... so you have the first result in the format of
//FROM THE MATRIX QUERY
array(
[0] => array(
"Store Number" => 123456,
"ITEM1" => 1,
"ITEM2" => 15
),
[1] => array(
"Store Number" => 678920,
"ITEM1" => 31,
"ITEM2" => 9
),
ETC...
)
//FROM YOUR ITEM QUERY
array(
[0] => array(
"type_description" => "ITEM1",
"quantity" => ???
),
[1] => array(
"type_description" => "ITEM2",
"quantity" => ???
)
ETC......
)
ok now you can do 2 foreach loops where you iterate through the 2 arrays
foreach(arrayMatrix as store){
foreach(arrayItems as item){
print "Store: "+ store['Store Number']+" ITEM: " + item['type_description']+" total: "+ (store[item['type_description']] * item['quantity']);
}
}
now I wrote this in psuedo code and Im not sure if you can translate this well into VB. With those 2 foreach or while loops or whatever you can databind the results to the grid or put them in another array or list... I hope this helps at all.