No such column error when column does exist - mysql

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

Related

Sql query Cross Transform or Pivot?

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'

Inner join without results from second table

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

Unknown column event_id in field list

I'm getting the error:
Unknown column event_id in field list
yet I have this column in both tables
SELECT
event_id.events,
tournament.events,
team1.events,
team2.events,event_date.events,
venue.events, picks.pick,tournament.picks
FROM events
INNER JOIN picks
ON `event_id`.events = `event_id`.picks
WHERE tournament = 'SUPER15'
I have checked and tweaked and tried but I just can't figure out what is wrong...
TABLE EVENTS
TABLE PICKS
First should be table name or alias than column name as below
SELECT
events.event_id,
events.tournament,
events.team1,
events.team2,
events.event_date,
events.venue,
picks.pick,
picks.tournament
FROM events
INNER JOIN picks ON
events.`event_id` = picks.`event_id`
WHERE
events.tournament = 'SUPER15'
You have to add column name for condition WHERE tournament = 'SUPER15' too. I changed it to WHERE events.tournament = 'SUPER15'. I also recommend you to use aliases as below
SELECT
E.event_id,
E.tournament,
E.team1,
E.team2,
E.event_date,
E.venue,
P.pick,
P.tournament
FROM events E
INNER JOIN picks P ON
E.`event_id` = P.`event_id`
WHERE
E.tournament = 'SUPER15'
It's more readable, isn't?
You're specifying your selections in reverse order. It should be table.column:
SELECT events.event_id,events.tournament, events.team, events.team2,
events.event_date, events.venue, pick.picks,picks.tournament
FROM events
INNER JOIN picks ON `event_id`.events = `event_id`.picks
WHERE tournament = 'SUPER15'

Taking one column from MySQL joined tables

I have a query in MySQL and I am making a crystal report by using this.
Now inside the query i have a column called scan_mode and it is coming from gfi_transaction table. This scan_mode I am using in report to suppress some sections. But some times this value is coming null for some transaction ids.
So now I want to take this scan_mode as separate query so that it will work.
Can any one please help how I can modify the below query to take only scan_mode column.
SELECT
cc.cost_center_code AS cccde,
cc.name AS ccnme,gf.scan_mode,
cc.cost_center_id AS ccid,
site.name AS siteme,
crncy.currency_locale AS currency_locale,
cntry.language AS LANGUAGE,
cntry.country_name AS cntrynm,
crncy.decimal_digits AS rnd,
gf.transaction_no AS Serial_No,
brnd.name AS brand_name,
rsn.description AS reason,
gf.comment AS COMMENT,
ts.status_description AS STATUS,
DATE_FORMAT(gf.created_date,'%d/%m/%Y') AS created_date,
gf.created_by AS created_by,
IFNULL(gf.approval_no,'Not authorized') AS Trans_no,
gf.approved_date AS approval_dt,
gf.approved_by AS approved_by,gf.status AS status1,
IFNULL(loc.cost_center_code,cc.cost_center_code) AS cur_location,
gf.document_ref_no,gf.document_ref_type,
,DATE_FORMAT(document_ref_date1,'%d/%m/%Y')) AS invoice_no
FROM
gfi_transaction gf
INNER JOIN gfi_instruction gfn ON (gf.transaction_id=gfn.transaction_id)
INNER JOIN gfi_document_instruction doc ON (gf.ref_transaction_no = doc.document_instruction_id)
INNER JOIN reason rsn ON (gf.reason_id = rsn.reason_id)
INNER JOIN gfi_status ts ON (gf.status = ts.gfi_status_id)
INNER JOIN transaction_type tt ON (gf.transaction_type_id = tt.transaction_type_id)
INNER JOIN brand brnd ON(gf.brand_id=brnd.brand_id)
-- cc details
INNER JOIN cost_center cc ON (brnd.parent_brand = cc.brand_id OR gf.brand_id = cc.brand_id)
INNER JOIN site site ON(cc.site_id = site.site_id)
INNER JOIN country cntry ON (site.country_id = cntry.country_id)
INNER JOIN currency crncy ON (cntry.currency_id=crncy.currency_id)
LEFT OUTER JOIN alshaya_location_details loc ON
(gf.brand_id = loc.brand_id AND loc.cost_center_id = gf.cost_centre_id)
LEFT OUTER JOIN alshaya_location_details locto ON
(locto.cost_center_id = gf.from_cost_center_id)
WHERE
gf.transaction_id='{?TransID}'
AND rsn.transaction_type_id IN (10,11,14)
wow, that's a big query. I ran across a similar problem in a query i was building and found the if syntax to be a solution to my problem. This was also answered in this question: MYSQL SELECT WITHIN IF Statement
$psdb->query = "SELECT count, s.classid,
if (k.sic != k.siccode, k.siccode, s.siccode) as siccode,
if (k.sic != k.siccode, k.sicdesc, s.sicdesc) as sicdesc,
if (k.sic != k.siccode, k.sicslug, s.sicslug) as sicslug
FROM ...
It looks like scan_mode column comes from "gfi_transaction" table which seems to be primary table in your query. If you get null for this column then it means your table itself have NULL value for this column. Taking that separately in a query wont solve your problem. Try replacing null with a default value and handle it in code. You can add default value instead of NULL by using ifnull(scan_mode, 'default')

Error with my left joins?

I'm thinking the coffee isn't strong enough today. I need to query the table fb-celebotd to get the photoid that matches the fb-celebotd.celebdate. Most of the other info needed is dependent on the photos table.
The following is giving me a mysql error:
Select photos.*,photographer.name, events.eventname, events.eventhome, subevents.subeventname, photodir.photodir, fb-celebotd.celebdate, fb-celebotd.trivia
from photos
LEFT JOIN fb-celebotd ON (photos.photoid = fb-celebotd.photoid)
LEFT JOIN photodir ON (photos.photodirid = photodir.photodirid)
LEFT JOIN photographer ON (photos.photographerid = photographer.photographerid)
LEFT JOIN events ON (photos.eventid = events.eventid)
LEFT JOIN subevents ON (photos.subeventid = subevents.subeventid)
WHERE fb-celebotd.celebdate=1277092800
Is this doable in one query or do I have to query fb-celebotd for the photoid and trivia first and then query other tables based on the photoid?
Thanks.
Should fb-celebotd (with a dash) be fb_celebotd (with an underscore)? If the table name has a dash, then you'll have to quote the table name: "fb-celebotd". Otherwise the dash is treated as a minus sign and tries to subtract celebotd from fb (both unknown).
If you really have a table named fb-celebotd, then you need to quote the table name every time you use it in a query:
Select photos.*,photographer.name, events.eventname, events.eventhome, subevents.subeventname, photodir.photodir, `fb-celebotd`.celebdate, `fb-celebotd`.trivia
from photos
LEFT JOIN `fb-celebotd` ON (photos.photoid = `fb-celebotd`.photoid)
LEFT JOIN photodir ON (photos.photodirid = photodir.photodirid)
LEFT JOIN photographer ON (photos.photographerid = photographer.photographerid)
LEFT JOIN events ON (photos.eventid = events.eventid)
LEFT JOIN subevents ON (photos.subeventid = subevents.subeventid)
WHERE `fb-celebotd`.celebdate=1277092800