Declare multiple variables for single variable in mysql - mysql

My query pulls list of bug items by jira issue (goes specific to component of jira), I need to find out how can i enter multiple pkey in this query, single pkey works fine, however when I declare multiple pkey it throws an error message.
In following query, abc-123 is a jira bug id, subquery finds component name and 2nd subquery will fetch project name, remaining query will pull list of bugs associated with the component, I would like to enter 'def-456', 'jdk-985' next to 'abc-123' , I tried setting new variable however it did not work, can someone please help
$
set #pkey := 'abc-123';
select jiraissue.*, co.*
from jiraissue,project,issuetype,nodeassociation,component,
customfieldvalue cv
,customfieldoption co
where
component.cname = (SELECT component.cname
FROM nodeassociation, component, jiraissue
WHERE component.ID = nodeassociation.SINK_NODE_ID
AND jiraissue.id = nodeassociation.SOURCE_NODE_ID
AND nodeassociation.ASSOCIATION_TYPE = 'IssueComponent'
AND pkey = #pkey) and
project.pkey = (SELECT substring_index(jiraissue.pkey,'-',1) as project_name
FROM nodeassociation, component, jiraissue
WHERE component.ID = nodeassociation.SINK_NODE_ID
AND jiraissue.id = nodeassociation.SOURCE_NODE_ID
AND nodeassociation.ASSOCIATION_TYPE = 'IssueComponent'
AND pkey = #pkey) and
issuetype.pname = 'Bug' and
jiraissue.project = project.id and
jiraissue.issuetype = issuetype.id and
nodeassociation.association_type = 'IssueComponent' and
nodeassociation.source_node_entity = 'Issue' and
nodeassociation.source_node_id = jiraissue.id and
nodeassociation.sink_node_entity = 'Component' and
nodeassociation.sink_node_id = component.id
and jiraissue.id = cv.issue
and cv.stringvalue = co.id
and cv.customfield = 10020;

Try replacing
AND pkey = #pkey
with this:
AND pkey in (#pkey, #pkey1, #pkey2)
and then at the top:
set #pkey := 'abc-123', #pkey1 := 'def-456', #pkey2 = 'ghi-789'

Related

Missing text from <memo> field in table in report

I have multiple models in Sparx Enterprise Architect in file-based, i.e. using MS access.
I'm using a custom template to populate a table with data from object's properties, including some with <memo> fields.
This is the query i'm using in the template fragment:
SELECT obj.object_id,
obj.Stereotype,
objp.Property as Prop,
switch(objp.Value = '<memo>', objp.Notes, objp.Value LIKE '{*}',
NULL, 1=1, objp.Value) AS Val,
(SELECT tobj2.ea_guid & tobj2.Name FROM t_object tobj2 WHERE
tobj2.ea_guid = objp.Value) AS [Obj-Hyperlink]
FROM t_object obj
INNER JOIN t_objectproperties objp
ON (obj.object_id = objp.object_id)
WHERE obj.object_id = #OBJECTID# AND obj.Stereotype='Data-
Stream' AND objp.Property NOT IN ('isEncapsulated')
ORDER BY objp.Property ASC;
I found that the when these fields are longer than 249 chars I get an error message when generating the reports and the cell in the generated table is simply empty. This is also noticeable with a query:
The error I'm getting states:
"Error Processing xml document: an invalid character was found in text context"
Is there any workarround to enable including the <memo> fields' data with more than 249 chars in the reports?
Any help is much appreciated.
I've found a workaround for this by joining two queries with a "Union all". The first query will handle the non-memo fields with the switch function and the second one the memo fields without the switch function.
select
obj.object_id,
obj.Stereotype,
objp.Property as Prop,
objp.Notes AS Val,
(
SELECT
tobj2.ea_guid & tobj2.Name
FROM
t_object tobj2
WHERE
tobj2.ea_guid = objp.Value
) AS [Obj-Hyperlink]
from
t_objectproperties objp
left join t_object obj on (obj.object_id = objp.object_ID)
where
obj.object_id = #OBJECTID#
AND obj.Stereotype = 'Data-Stream'
AND objp.Property NOT IN ('isEncapsulated')
AND objp.Value = "<memo>"
UNION ALL
SELECT
obj2.object_id,
obj2.Stereotype,
objp2.Property as Prop,
switch(
objp2.Value LIKE '{*}', NULL, 1 = 1, objp2.Value
) AS Val,
(
SELECT
tobj2.ea_guid & tobj2.Name
FROM
t_object tobj2
WHERE
tobj2.ea_guid = objp2.Value
) AS [Obj-Hyperlink]
FROM
t_object obj2
INNER JOIN t_objectproperties objp2 ON (obj2.object_id = objp2.object_id)
WHERE
obj2.object_id = #OBJECTID#
AND obj2.Stereotype = 'Data-Stream'
AND objp2.Property NOT IN ('isEncapsulated')
and objp2.Value <> "<memo>"
order by
3 asc;
Thanks a lot #geertbellekens for your comment which was crucial to find this solution.

UPDATE random result numbers to same numbers result

i'm trying to update column from another table which means :
SELECT DISTINCT id FROM creature WHERE map = 389;
This SQL will give me this result :
11323
11322
11324
11520
11321
What i want is to update creature_template lootid = 11323 where entry = 11323 so it goes as following :
UPDATE creature_template SET lootid = 11323 WHERE entry = 11323
I have tried this :
UPDATE creature_template SET lootid =
(SELECT DISTINCT id
FROM creature
WHERE map = 389) WHERE lootid = entry;
I'm sure it's incorrect simply it's not logic but couldn't find the logical answer for this.
Even REPLACE could work instead of UPDATE so any will work.
You need JOIN with UPDATE :
UPDATE creature_template ct
INNER JOIN creature c
ON c.id = ct.entry
SET ct.lootid = c.id
WHERE c.map = 389;

Can I use the query input parameter to have different where condiction? (something like an IF statment)

I am not so into SQL and I have the following problem woring on a MySql query. I try to explain you what I have to do.
I have this query, it works fine:
SELECT
LNG.id AS language_id,
LNG.language_name AS language_name,
LNG.language_code AS language_code,
CLP.is_default AS id_default_language
FROM Country_Language_Preference AS CLP
INNER JOIN Country AS CNT
ON CLP.country_id = CNT.id
INNER JOIN Languages AS LNG
ON CLP.language_id = LNG.id
WHERE
CNT.country_name = "Senegal"
This query have a single WHERE input parameter, this:
CNT.country_name = "Senegal"
I want to implement the following behavior: if the passed parameter have value Senegal or Rwanda perform the previous query.
If this input parameter have a different value form Senegal or Rwanda perform the same query but using this WHERE condition_
CNT.country_name = "GLOBAL"
Can I do something like this using SQL?
Using CASE Statement, this should be possible.
Try this:
SELECT
LNG.id AS language_id,
LNG.language_name AS language_name,
LNG.language_code AS language_code,
CLP.is_default AS id_default_language
FROM Country_Language_Preference AS CLP
INNER JOIN Country AS CNT
ON CLP.country_id = CNT.id
INNER JOIN Languages AS LNG
ON CLP.language_id = LNG.id
WHERE
CNT.country_name = CASE WHEN #Country = "Senegal" OR #Country = "Rwanda" THEN "Senegal"
ELSE "GLOBAL" END
Simply use OR:
WHERE (CNT.country_name = #country OR #country = 'GLOBAL')
#country is whatever parameter you are passing in.
If you want to limit to those two countries, then:
WHERE (CNT.country_name = #country OR
(#country NOT IN ('Rwanda', 'Senegal') AND CNT.country_name = 'GLOBAL')
)
But the first version seems more versatile.

HOW do i do a data append against multiple columns

I added a column into table A and right now it is empty. What i want to do is take the phone column from table consumer and input it into appphone of table diabetic as long as the first name, last name, address, city, state, and zip, match up in both tables. Below is the query i have been trying and in theory should work but is not. I keep getting the same error no matter which way i change the query.--
error 'subquery returns more than one row'
UPDATE Diabetic_DB
SET Diabetic_DB.AppPhone = (SELECT Consumer.PHONE FROM Consumer
WHERE Consumer.FN = Diabetic_DB.FirstName
and Consumer.LN = Diabetic_DB.LastName and Consumer.ADDR = Diabetic_DB.Address1
and Consumer.CITY = Diabetic_DB.City and Consumer.ST = Diabetic_DB.State
and Consumer.ZIP = Diabetic_DB.Zip)
WHERE EXISTS (SELECT DISTINCT(PHONE) FROM Consumer WHERE Consumer.FN = Diabetic_DB.FirstName
and Consumer.LN = Diabetic_DB.LastName and Consumer.ADDR = Diabetic_DB.Address1
and Consumer.CITY = Diabetic_DB.City and Consumer.ST = Diabetic_DB.State
and Consumer.ZIP = Diabetic_DB.Zip)
the original query i ran looked like this.
UPDATE Diabetic_DB
SET Diabetic_DB.AppPhone = Consumer.PHONE
WHERE EXISTS (SELECT * FROM Consumer WHERE Consumer.FN = Diabetic_DB.FirstName
and Consumer.LN = Diabetic_DB.LastName and Consumer.ADDR = Diabetic_DB.Address1
and Consumer.CITY = Diabetic_DB.City and Consumer.ST = Diabetic_DB.State
and Consumer.ZIP = Diabetic_DB.Zip)
Try something like this:
UPDATE Diabetic_DB
INNER JOIN Consumer ON
Consumer.FN = Diabetic_DB.FirstName
and Consumer.LN = Diabetic_DB.LastName and Consumer.ADDR = Diabetic_DB.Address1
and Consumer.CITY = Diabetic_DB.City and Consumer.ST = Diabetic_DB.State
and Consumer.ZIP = Diabetic_DB.Zip
SET Diabetic_DB.AppPhone = Consumer.PHONE

MySQL UPDATE only if field in other table meets condition

I am having a little trouble trying to accomplish this. Here is the gist of what I need to do:
UPDATE links SET
link = '$link', rid = $rid, order = $order
WHERE lid = $lid
IF (SELECT COUNT(*) FROM resources WHERE rid = $rid AND (sid = $sid OR sid IS NULL) AND types IS NULL) == 1;
So basically, I want to run the UPDATE if and only if the resource in the resources table is associated with the site (sid) or is not associated with any particular site and types is null.
I suppose I can run a PHP conditional, but it would be preferable if I could do this with one query. Is it possible?
Thansk so much in advance!
UPDATE links
SET link = '$link', rid = $rid, order = $order
WHERE lid = $lid
and (SELECT COUNT(*)
FROM resources
WHERE rid = $rid
AND (sid = $sid OR sid IS NULL)
AND types IS NULL) = 1;