I have this query for MySql database:
SELECT
pim_pimcore_database.object_query_RGL.rulecode AS "rulecode",
pim_pimcore_database.object_query_RGL.DescrizioneRegola AS "rule_desc",
pim_pimcore_database.object_relations_RGL.position AS "id",
pim_pimcore_database.objects.o_classId,
pim_pimcore_database.objects.o_key,
pim_pimcore_database.object_collection_ruleIF_RGL.Operatore,
pim_pimcore_database.object_collection_ruleIF_RGL.Concatenatore
FROM
pim_pimcore_database.object_query_RGL
LEFT JOIN pim_pimcore_database.object_relations_RGL
ON pim_pimcore_database.object_query_RGL.oo_id =
pim_pimcore_database.object_relations_RGL.src_id
LEFT JOIN pim_pimcore_database.objects
ON pim_pimcore_database.object_relations_RGL.dest_id = pim_pimcore_database.objects.o_id
LEFT JOIN pim_pimcore_database.object_collection_ruleIF_RGL
ON pim_pimcore_database.object_relations_RGL.position =
pim_pimcore_database.object_collection_ruleIF_RGL.`index`
AND pim_pimcore_database.object_relations_RGL.src_id =
pim_pimcore_database.object_collection_ruleIF_RGL.o_id
GROUP BY
pim_pimcore_database.object_relations_RGL.position AS "ID"
WHERE
pim_pimcore_database.object_query_RGL.oo_id = 1042 AND
pim_pimcore_database.object_relations_RGL.ownername ="IfStatement"
That gives me the result:
But what I need is table like this:
How do I change the query for the desired output?
Your cartesian product (doubling of the rows) appears to come from joining pim_pimcore_database.objects on o_id.
If instead you represent this table twice in the query (join it twice) with relevant aliases and a predicate that selects only those kind of o_classid relevant to that alias, then the cartesian will disappear and you'll get the result you seek:
SELECT
...,
o_opt.key as OPT,
o_com.key as COM,
...
LEFT JOIN pim_pimcore_database.objects o_opt ON ... AND o_opt.o_classid = 'OPT'
LEFT JOIN pim_pimcore_database.objects o_com ON ... AND o_com.o_classid = 'COM'
Related
How to write this query in codeigniter
SELECT U.username,U.user_id
FROM storylikes S, user U
WHERE U.user_id=S.user_id_fk AND S.user_id_fk='$id'
try this :
$this->db->select('u.username, u.user_id');
db->where('u. user_id = s.user_id_fk');
$this->db->where('s.user_id_fk = '.$id);
$query = $this->db->get('storylikes s, user u');
use $your_variable = $query->result(); for the result
you should use joins instead of this query
$this->db->select('username,user_id');
$this->db->from('user');
$this->db->join('storylike','storylike.user_id_fk = user.user_id');
$this->db->where('storylike.user_id','$id');
as long as the db helper is loaded... You dont need to do anything special
$query = $this->db->query('SELECT U.username,U.user_id FROM storylikes S, user U WHERE U.user_id=S.user_id_fk AND S.user_id_fk=$id);
Using a cartesian (cross join) by doing FROM with 2 tables can cause some unruly results if not used 'correctly'
I suggest that if you are trying to just join tables together your SQL should be
SELECT U.username,U.user_id
FROM storylikes S, user U
INNER JOIN user U ON S.user_id = U.user_id_fk
WHERE S.user_id_fk=$id
CI querybuilder for this would be:
$query = $this->db->select('U.username,U.user_id')
->join('user U', 'S.user_id = U.user_id_fk', 'inner')
->where('S.user_id', $id)
->get('user U');
Using the correct join for the correct requirements is key;
INNER JOIN to ensure both FROM and the JOIN table match 1 for 1...
LEFT JOIN if you want to ensure you have all data from your FROM table and any without results in the JOIN table show up as NULL
RIGHT JOIN (opposite of left), to grab all data from the JOIN table and only matching data from the FROM table.
CROSS (CARTESIAN) JOIN when you want to ... frankly... mash the data together... A CROSS JOIN will also function like an INNER JOIN when you stipulate criteria in the WHERE statement (like you did) but still, use the correct JOIN for the correct usage-case.
There are other available joins but those are the basics.
I have two tables named DE1_0_2008_TO_2010_OUTPATIENT_CLAIMS_SAMPLE_1 and DE1_0_2008_BENEFICIARY_SUMMARY_FILE_SAMPLE_1 in the same database.
I have to perform a query on DE1_0_2008_TO_2010_OUTPATIENT_CLAIMS_SAMPLE_1 and then perform the LEFT JOIN of this queried result with `DE1_0_2008_BENEFICIARY_SUMMARY_FILE_SAMPLE_1'. The query alone results in 1178 rows which is fine. But I am not able to do the LEFT JOIN. I used this SQL query:
SELECT *
FROM `DE1_0_2008_TO_2010_OUTPATIENT_CLAIMS_SAMPLE_1`
LEFT JOIN DE1_0_2008_BENEFICIARY_SUMMARY_FILE_SAMPLE_1
ON DE1_0_2008_TO_2010_OUTPATIENT_CLAIMS_SAMPLE_1.DESYNPUF_ID = DE1_0_2008_BENEFICIARY_SUMMARY_FILE_SAMPLE_1.DESYNPUF_ID
WHERE DE1_0_2008_TO_2010_OUTPATIENT_CLAIMS_SAMPLE_1.ICD9_DGNS_CD_1 = 7243 OR DE1_0_2008_TO_2010_OUTPATIENT_CLAIMS_SAMPLE_1.ICD9_DGNS_CD_2 = 7243 OR DE1_0_2008_TO_2010_OUTPATIENT_CLAIMS_SAMPLE_1.ICD9_DGNS_CD_3 = 7243 OR ICD9_DGNS_CD_4 = 7243
This query is technically fine.
Note that it can be rewritten more elegantly as follows, but really you need to take a closer look at both partitioning and, crucially, normalisation...
SELECT *
FROM DE1_0_2008_TO_2010_OUTPATIENT_CLAIMS_SAMPLE_1 o
LEFT
JOIN DE1_0_2008_BENEFICIARY_SUMMARY_FILE_SAMPLE_1 b
ON o.DESYNPUF_ID = b.DESYNPUF_ID
WHERE 7243 IN(o.ICD9_DGNS_CD_1,o.ICD9_DGNS_CD_2,o.ICD9_DGNS_CD_3,o.ICD9_DGNS_CD_4);
I am trying to create an inner-join MySQL Query of two tables:
'swatchset'
->swatchset_id
->swatchset_name
'swatches'
->swatch_id
->swatch_name
->swatch_hex
->etc...
I currently have one record in the 'swatchset' table:
swatchset_id | swatchset_name
8 default
The 'swatches' table is empty.
I want to get as result the 'swatchset'.'swatchset_name'
I guess I don't understand INNER JOIN very well because this Query results nothing:
SELECT `swatchset`.`swatchset_name`, `swatches`.`swatch_id`, `swatches`.`swatch_name`, `swatches`.`swatch_hex`, `swatches`.`swatch_type` FROM `swatchset`
INNER JOIN `swatches`
ON `swatches`.`f_swatchset_id` = `swatchset`.`swatchset_id`
WHERE `swatchset`.`swatchset_id` = '8';
How can I get at least the found 'swatchset' row as result?
Left outer join will fill with nulls columns of second table when not found:
SELECT `swatchset`.`swatchset_name`, `swatches`.`swatch_id`, `swatches`.`swatch_name`, `swatches`.`swatch_hex`, `swatches`.`swatch_type` FROM `swatchset`
LEFT OUTER JOIN `swatches`
ON `swatches`.`f_swatchset_id` = `swatchset`.`swatchset_id`
WHERE `swatchset`.`swatchset_id` = '8';
inner join doesn't return entries from first table without matching ones in second table
I have three tables: nanoProd, nanoFiles, and nanoRelFiles. Third table is used to store the file meta and how they relate to other screens.
I'm getting an error that a column doesn't exist when I know it does and I'm not sure why:
no such column: nanoFiles.fileLoc:
SELECT
prodTable.name AS prodName,
prodTable.intro AS prodIntro,
prodTable.prodText AS nanoText,
nanoFiles.fileLoc AS nanoFile
FROM nanoProd AS prodTable
LEFT JOIN nanoRelFiles on nanoFiles.rid = nanoRelFiles.file_id
LEFT JOIN nanoProd ON nanoProd.rid = nanoRelFiles.item_id
WHERE nanoRelFiles.scr_type = 'prod' AND nanoRelFiles.fileUse = 'list'
You aren't joining to any table called "nanoFiles." You need to JOIN to that table to be able to SELECT from that column. Something like this:
FROM nanoProd AS prodTable
JOIN nanoFiles on ...
LEFT JOIN nanoRelFiles on nanoFiles.rid = nanoRelFiles.file_id
I am currently running this SQL
SELECT jm_recipe.name, jm_recipe.slug
FROM jm_recipe
LEFT JOIN jm_category_recipe ON jm_category_recipe.recipe_id = jm_recipe.id
WHERE jm_category_recipe.category_id = $cat"
This returns the desired results except that I also need to return the name of the category that the recipe I am looking for is in, to do this I tried to add the field in to my SELECT statement and also add the table into the FROM clause,
SELECT jm_recipe.name, jm_recipe.slug, jm_category_name
FROM jm_recipe, jm_category
LEFT JOIN jm_category_recipe ON jm_category_recipe.recipe_id = jm_recipe.id
WHERE jm_category_recipe.category_id = $cat"
However this just returns no results, what am i doing wrong?
You need to join both tables:
SELECT jm_recipe.name, jm_recipe.slug, jm.category_name
FROM jm_recipe
INNER JOIN jm_category_recipe ON jm_category_recipe.recipe_id = jm_recipe.id
INNER JOIN jm_category ON jm_recipe.recipe_id = jm_category.recipe_id
WHERE jm_category_recipe.category_id = $cat
I've changed the joins to inner joins as well. You might want to make them both LEFT joins if you have NULLs and want them in the result.
Also, you're vulnerable to SQL Injection by simply copying over $cat.
Here's some PHP specific info for you (I'm assuming you're using PHP.)