Doctrine many to many left join - symfony

I have a problem with creating a query which should return cost centers not assigned to budget.
Database structure:
**Cost_center:**
+------+-----------+
| id | title |
+------+-----------+
| (PK) | (VARCHAR) |
+------+-----------+
\/
One
to
many
\/
**Budget_operation_scope_cost_center:**
+----------------+---------------------------+
| cost_center_id | budget_operation_scope_id |
+----------------+---------------------------+
| (FK) | (FK) |
+----------------+---------------------------+
\/
Many
to
one
\/
**Budget_operation_scope:**
+------+-----------+-----------+
| id | title | budget_id |
+------+-----------+-----------+
| (PK) | (VARCHAR) | (FK) |
+------+-----------+-----------+
\/
Many
to
one
\/
**Budget:**
+------+-------+
| id | year |
+------+-------+
| (PK) | (INT) |
+------+-------+
Managed to do a query which returns assigned to budget cost centers list:
$query = $this->getEntityManager()
->createQueryBuilder()
->select('costCenter')
->from('ResourcesBundle:CostCenter', 'costCenter')
->leftJoin('costCenter.budgetOperationScope', 'budgetOperationScope')
->where('budgetOperationScope.budgetId = :budget')
->setParameter('budget', $budget)
->getQuery()->getResult();
Question: how to get cost centers, which are not assigned to budget?

This line is constraining your query too early and effectively making your left join a join:
->where('budgetOperationScope.budgetId = :budget')
You can move it into your left join like so:
->leftJoin('costCenter.budgetOperationScope', 'budgetOperationScope', 'WITH' 'budgetOperationScope.budgetId = :budget')
This way you will now get null rows for budgetOperationScope when a cost center has no budget.
So you can effectively do:
->where('budgetOperationScope IS NULL')
All together:
$query = $this->getEntityManager()
->createQueryBuilder()
->select('costCenter')
->from('ResourcesBundle:CostCenter', 'costCenter')
->leftJoin('costCenter.budgetOperationScope', 'budgetOperationScope', 'WITH' 'budgetOperationScope.budgetId = :budget')
->where('budgetOperationScope IS NULL')
->setParameter('budget', $budget)
->getQuery()->getResult();

Related

Storing attributes with multiple values in relational database

I am storing product attributes in a relational table in a MariaDB database the following way:
I have a main table, called Products which provide the name, description, and other simple information about a product, and another table, ProductAttributes, with the following structure: Id|ProductId|Attribute|Value where Id is an autoincremented primary key, and ProductId is a reference to a row in the Products table.
I can store simple attribute value relations to a product in this way, say ie, height, weight, length of a product. My problems start, when a product's attribute, ie color can have multiple possible values.
I could add multiple lines to the ProductAttributes table when storing multi-valued attributes, ie:
1|yy|color|red
2|yy|color|blue
and from this schema, I could easily retrieve a single product's attributes, but I am having trouble on how to go forward when trying to compare two products based on their attributes.
Is there any other way to store multiple values for a single attribute in a relational database to maintain their searchability?
As of now, to find similar attributed products I am doing a similar query:
SELECT * FROM ProductAttributes base
INNER JOIN ProductAttributes compare ON compare.ProductId != base.ProductId
WHERE base.Attribute = compare.Attribute
AND base.Value = compare.Value
AND base.ProductId = 'x'
GROUP BY compare.ProductId
My problem is, that this query will return the products with a red and blue color, as similar to products with a blue color.
Btw, I can not change my attributes tables to a one attribute per column representation, because I do not know from the get-go how many attributes will I have, and even if I knew, I have way too many possible attributes and differences on each product category, to represent this in a traditional table.
A possible pitfall is, that I also want to compare products to one another with missing attributes. Ie, if a product has a length attribute specified, but another one has no length attribute, they could still be similar. Right now, to make this kind of comparison, in the background, I am transposing my attributes table, to a simple table, and on that table, perform this query:
SELECT b.ProductId as BaseProduct, s.ProductId as SimProduct
FROM tmp_transposed_product_attributes b
CROSS JOIN tmp_transposed_product_attributes s ON b.ProductId != s.ProductId
WHERE (b.attribute1 = s.attribute1 OR b.attribute1 IS NULL OR s.attribute1 IS NULL)
AND (b.attribute2 = s.attribute2 OR b.attribute2 IS NULL OR s.attribute2 IS NULL) ...
If I'm following correctly for the product comparison, I like to use EXISTS or NOT EXISTS to help find things like that, which may also help avoid having to transpose the data.
For example, given this sample table data:
MariaDB [test]> select * from productattributes;
+----+-----------+-----------+-------+
| id | productID | attribute | value |
+----+-----------+-----------+-------+
| 1 | yy | height | 5 |
| 2 | yy | color | red |
| 3 | yy | weight | 10 |
| 4 | yy | length | 6 |
| 5 | yy | color | blue |
| 6 | zz | color | white |
| 7 | zz | height | 5 |
| 8 | zz | length | 8 |
+----+-----------+-----------+-------+
8 rows in set (0.00 sec)
To find all similar attributes between the two, but has different values (removes attribute/values pairs that are the same) use a NOT EXISTS query to same table like so:
MariaDB [test]> SELECT * FROM `productattributes` pA
-> WHERE productID IN ('yy', 'zz')
-> AND NOT EXISTS (SELECT * FROM productattributes pB
-> WHERE pA.attribute = pB.attribute
-> AND pA.value = pB.value
-> AND pA.productID != pB.productID)
-> ORDER BY productID, attribute;
+----+-----------+-----------+-------+
| id | productID | attribute | value |
+----+-----------+-----------+-------+
| 2 | yy | color | red |
| 5 | yy | color | blue |
| 4 | yy | length | 6 |
| 3 | yy | weight | 10 |
| 6 | zz | color | white |
| 8 | zz | length | 8 |
+----+-----------+-----------+-------+
6 rows in set (0.00 sec)
Then to find attribute/value pairs that ARE the same between the two, simply remove the NOT portion of the query:
MariaDB [test]> SELECT * FROM `productattributes` pA
-> WHERE productID IN ('yy', 'zz')
-> AND EXISTS (SELECT * FROM productattributes pB
-> WHERE pA.attribute = pB.attribute
-> AND pA.value = pB.value
-> AND pA.productID != pB.productID)
-> ORDER BY productID, attribute;
+----+-----------+-----------+-------+
| id | productID | attribute | value |
+----+-----------+-----------+-------+
| 1 | yy | height | 5 |
| 7 | zz | height | 5 |
+----+-----------+-----------+-------+
2 rows in set (0.00 sec)
Here's the query without the command line junk:
SELECT * FROM `productattributes` pA
WHERE productID IN ('yy', 'zz')
AND NOT EXISTS (SELECT * FROM productattributes pB
WHERE pA.attribute = pB.attribute
AND pA.value = pB.value
AND pA.productID != pB.productID)
ORDER BY productID, attribute;
EDIT:
To cover the case where there is an attribute that is in one but not the other, then the value check of the query can be removed:
MariaDB [test]> SELECT * FROM `productattributes` pA
-> WHERE productID IN ('yy', 'zz')
-> AND NOT EXISTS (SELECT * FROM productattributes pB
-> WHERE pA.attribute = pB.attribute
-> AND pA.productID != pB.productID)
-> ORDER BY productID, attribute;
+----+-----------+-----------+-------+
| id | productID | attribute | value |
+----+-----------+-----------+-------+
| 3 | yy | weight | 10 |
+----+-----------+-----------+-------+
1 row in set (0.00 sec)

sqlite extract two row from two tables to create a new table

I have an sqlite db with two table
table1
------------------------------
TIME | ElevationA| ElevationB|
-----|-----------|-----------|
T1 | eA1 | eB1  |
T2 | eA2 | eB2 |
table2
------------------------------
TIME | Temperat A| Temperat B|
-----|-----------|-----------|
T1 | tA1 | tB1  |
T2 | tA2 | tB2 |
I am searching for a "magic" command that make a table of all parameter at a given time, e.g something that would be like:
SELECT WHERE TIME=T1 table1 AS ELEV ,table2 AS TEMP
and that would result in
table3
------------
ELEV | TEMP |
-----|----- |
eA1 | tA1 |
eB1 | tB1 |
Of course I could bash script it but I would prefer a to create a view in SQLite as it is more straightforwards and avoid to duplicate the data.
Any idea welcome
You can use:
CREATE TABLE TABLE3(ELEV,TEMP);
INSERT INTO TABLE3(ELEV,TEMP) VALUES((SELECT TIME FROM TABLE1 WHERE TIME = T1),SELECT TIME FROM TABLE2 WHERE TIME =T2));
These 2 select clauses must return the same number of records.

Oracle 11g: Replace part of string using dictionary mapping

Is there any nice trick to change values in string using dictionary mapping? For example I have table1 FIDDLE
+---------------------------+
| ROW1 |
+---------------------------+
| This is an example string |
| This String has typ0s |
+---------------------------+
And some mapping table dict1 FIDDLE:
+-------------------------+
| OLD | NEW |
+-------------------------+
| THIS | THAT |
| IS | ARE |
| EXAMPLE | SOURCE |
| STRING | NUMBER |
+------------+------------+
I need some SELECT statement that will split values in table1.row1 and change words using mapping dictionary dict1 so received values will be ( changing no existing dictionary values to upper is optional):
+---------------------------+
| TRANS_ROW1 |
+---------------------------+
| THAT ARE AN SOURCE NUMBER |
| THAT NUMBER HAS TYP0S |
+---------------------------+
PS. Spliting using REGEXP expression will be so nice..
WITH dict1 AS
(SELECT 'THIS' fr,
'THAT' t
FROM dual
UNION ALL
SELECT 'IS' fr,
'ARE' t
FROM dual
UNION ALL
SELECT 'EXAMPLE' fr,
'SOURCE' t
FROM dual
UNION ALL
SELECT 'STRING' fr,
'NUMBER' t
FROM dual),
table1 AS
(SELECT 'This is an example string' AS str,
1 AS sn
FROM dual
UNION ALL
SELECT 'This String has typ0s' AS str,
2 sn
FROM dual),
src AS
(SELECT regexp_substr(upper(s.str), '[^ ]+', 1, LEVEL) str2,
s.*,
rownum nn
FROM table1 s
CONNECT BY instr(TRIM(' ' FROM str), ' ', 1, LEVEL - 1) > 0
AND PRIOR sn = sn
AND PRIOR dbms_random.value IS NOT NULL),
repl AS
(SELECT nvl2(dict1.t, dict1.t, src.str2) lex,
sn,
nn
FROM src
LEFT JOIN dict1
ON dict1.fr = src.str2)
SELECT listagg(lex, ' ') within GROUP(ORDER BY nn),
sn
FROM repl
GROUP BY sn
It works now as you ask. Enjoy.
EDIT: FIDDLE with solution

Sqllite query on distinct or certain value

I have an android sqllite database. It has a text column called chainid.
I'd like to return all columns from rows with DISTINCT chainids || or where chainid is equal to: "none".
So e.g.:
| ID| Name | chainid |
| 1 | widgetname1 | 12345 |
| 2 | widgetname2 | 12345 |
| 3 | widgetname3 | "none" |
| 4 | widgetname4 | 49390 |
| 5 | widgetname5 | 49390 |
Given the above table I would like my query to return 3 rows with all columns for row 2, row3 and row5. -- So DISTINCT on chainid OR where chainid = "none" with the max id selected as the distinct row
Can I achieve this in one query?
I could return all and then process afterwards in java, but this is inefficient.
What about
select *
from table where id in
( select max(id)
from table
group by chainid
where chainid != 'none'
union
select id
from table
where chainid = 'none'
)

sqlite combining select results for searching

Alright so here are my two tables.
CREATE TABLE [cards] (
[id] TEXT PRIMARY KEY,
[game_id] TEXT NOT NULL,
[set_id] TEXT CONSTRAINT [id_set_id] REFERENCES [sets]([id]) ON DELETE CASCADE ON UPDATE CASCADE MATCH SIMPLE NOT DEFERRABLE INITIALLY IMMEDIATE,
[name] TEXT NOT NULL,
[image] TEXT NOT NULL);
CREATE TABLE [custom_properties] (
[id] TEXT PRIMARY KEY,
[card_id] TEXT CONSTRAINT [id_card_id] REFERENCES [cards]([id]) ON DELETE CASCADE ON UPDATE CASCADE MATCH SIMPLE NOT DEFERRABLE INITIALLY IMMEDIATE,
[game_id] TEXT CONSTRAINT [id_game_id4] REFERENCES [games]([id]) ON DELETE CASCADE ON UPDATE CASCADE MATCH SIMPLE NOT DEFERRABLE INITIALLY IMMEDIATE,
[name] TEXT NOT NULL,
[type] INTEGER NOT NULL,
[vint] INTEGER,
[vstr] TEXT);
What I would like to do is to have a search that grabs all the data from the cards row, and then adds the column who's name is (where custom_properties.card_id == cards.id).name.
Then I would like it's value to be vint if type == 1, else vstr.
So, here is an example dataset
cards
|id | game_id | set_id | name | image|
+---+---------+--------+------+------+
| a | asdf | fdsaf |loler | blah |
+------------------------------------+
custom_properties
| id | card_id | game_id | name | type | vint | vstr |
+----+---------+---------+------+------+------+------+
| f | a | asdf | range| 1 | 12 | |
| b | a | asdf | rank | 0 | | face |
+----+---------+---------+------+------+------+------+
the resulting table would look like this, where the columns range and rank are derived from custom_properties.name
|id | game_id | set_id | name | image | range | rank |
+---+---------+--------+------+-------+-------+------+
|a | asdf | fdsaf | loler| blah | 12 | face |
+---+---------+--------+------+-------+-------+------+
try this:
SELECT Cards.id,
Cards.game_id,
Cards.set_id,
Cards.name,
Cards.id,
Cards.image,
CASE
WHEN Custom_Properties.type = 1 THEN
Custom_Properties.vint
ELSE
Custom_Properties.vstr
END as Range
Custom_Properties.vstr as rank
FROM Cards INNER JOIN Custom_Properties
ON Cards.id = Custom_Properties.card_ID
WHERE Cards.Name = 'loller'
It's not actually possible to do this.

Resources