Please help me to write correct query for a few tables. I need to replace all id here from another table
api json
I am trying to make query like this
SELECT incident.`number`, `user`.first_name, (SELECT `user`.first_name from ITSM.`user` JOIN incident on `user`.sys_id = incident.id_created_by) as createdby
from ITSM.incident
JOIN ITSM.`user` on incident.id_caller = `user`.sys_id
;*
but it doesn#t work, I got an error: Subquery returns more than 1 row
How can i make a right query?
This one doesn't work also, same error:
SELECT incident.`number`, (SELECT user.first_name from ITSM.`user`, ITSM.incident WHERE user.sys_id = incident.id_created_by) as createdby
from ITSM.incident
JOIN ITSM.`user` on incident.id_caller = user.sys_id*
;
and this is my
DB id for user who created
You don't need a subquery. Just refer to the source table of each column in the select clause, here if you need 2 joins to the same table give these an alias and refer to the columns using that alias.
SELECT incident.`number`
, caller.first_name as caller_name
, creator.first_name AS createdby
FROM ITSM.incident
JOIN ITSM.`user` AS caller ON incident.id_caller = caller.sys_id
JOIN ITSM.`user` AS creator ON incident.id_created_by = creator.sys_id
Nb. I'm assuming your join logic is correct
Related
This is the query that is causing the problem:
CREATE TEMPORARY TABLE fetchedCropVariety ENGINE = MEMORY
SELECT *
, hz.zoneName
, i.fullImageUrl
, i.previewImageUrl
, u.userName
FROM seedrecord s
LEFT
JOIN HardinessZone hz
ON hz.id = s.hardiness_zone_id
JOIN image i
ON s.id = i.seedrecord_id
JOIN members u
ON s.FK_USER = u.id
WHERE s.id = 1
AND s.deleted = FALSE;
When i execute this query it throws the error Error Code: 1060. Duplicate column name 'id'
There are no duplicate columns named id in the table seedrecord so this is caused by one or more id columns from the other tables that are only used to join them in order to retrieve zoneName and fullImageUrl.
When i remove *, from the query the query does run but all the fields from seedrecord are omitted. It must be the id column from seedrecord that is clashing somehow with the id columns from hardinessZone and members but I am not trying to include the id fields of those 2 tables into the temporary table i want to create so this error is really puzzeling me.
Can anyone tell me what i can do to stop this error from occurring?
Thank you
don't make
select *
It will take all clomuns that you have in your From Clause
And you have the at least two Columns that are namend id
MAke instead
S.*,u.*
and so on
I have 3 tables that I am using and need to make a query to return data from one table based on the value of a single column in the second table.
tbl_user
ID
login
pass
active
mscID
tbl_master
ID
name
training_date
MSCUnit
Active
tbl_msc
mscID
mscName
my current SQL statement:
SELECT
tbl_master.ID,
tbl_master.name,
tbl_master.training_date,
tbl_master.MSCUnit,
tbl_master.active,
tbl_user.mscID
FROM
tbl_master,
tbl_user
WHERE
tbl_master.active = 1 AND tbl_master.MSCUnit = tbl_user.mscID
The values stored in tbl_msc.mscID is a varchar(11) and it contains a string similar to A00 or A19. This is also the Primary key in the table.
The values stored in tbl_user.mscID matches that of tbl_msc.mscID. The values stored in tbl_master.UnitMSC also matches that of tbl_msc.mscID.
My goal is to return all records from tbl_master where the currently logged in user has the same mscID. The problem I am having is the statement returns all records in tbl_master.
I have tried several different join statements and for some reason, I cannot get this to filter correctly.
I am missing something. Any assistance in the SQL statement would be appreciated.
Thanks,
Will
You should be writing this using joins. I don't know how you know who the current user is, but the idea is to join the three tables together:
SELECT m.ID, m.name, m.training_date, m.MSCUnit, m.active,
u.mscID
FROM tbl_master m JOIN
tbl_user u
ON m.MSCUnit = u.mscID JOIN
tbl_msc msc
ON msc.mscID = u.msc_ID
WHERE m.active = 1 AND msc.mscName = ?;
Notice the use of proper, explicit, standard JOIN syntax and table aliases.
Select a.*, b.userid from
table_master a, table_user b where
a.mscunit in (select mscid from
table_user where active=1)
This should point you in the right direction.
I have a database with 2 tables:
"services" and "service_performance"
Those 2 tables have a SERVICE_ID column.
In "services" the SERVICE_ID values are unique (each service has a single ID/entry).
In "service_performance" there is an AVERAGE_MEMORY column with multiple entries per service_id
I am trying to update the MAX_VALUE column in "services" table with the highest AVERAGE_MEMORY value taken from the "service_performance" table.
I know my query is wrong because it throws an error:
1054 - Unknown column 'service_performance.SERVICE_ID' in 'where clause'
While 'service_performance.SERVICE_ID' does exist.
Here is my query:
update _services
set MAX_VALUE = (SELECT MAX(AVERAGE_MEMORY) AS SERVICE_ID FROM service_performance)
where exists
(select *
from services
where `services`.`SERVICE_ID` = `service_performance`.`SERVICE_ID`);
You should find that this version works in MySQL:
update services s join
(select service_id, MAX(AVERAGE_MEMORY) as maxmem
from service_performance
group by service_id
) sp
on s.service_id = sp.service_id
set s.MAX_VALUE = sp.maxmem;
Your version would work if it had the right table name in the where clauses:
update services
set MAX_VALUE = (SELECT MAX(AVERAGE_MEMORY) AS SERVICE_ID
FROM service_performance
WHERE `services`.`SERVICE_ID` = `service_performance`.`SERVICE_ID`)
where exists (select *
from service_performance
where `services`.`SERVICE_ID` = `service_performance`.`SERVICE_ID`
);
I am assuming that update _services is a typo and should really be update services.
MySQL is complaining because in this query there is no service_performance table
Try it like this:
UPDATE _services s INNER JOIN service_performance p
ON s.SERVICE_ID=p.SERVICE_ID
SET s.MAX_VALUE=MAX(p.AVERAGE_MEMORY) GROUP BY p.SERVICE_ID
I don't have a database to test on, but firstly you haven't given the subquery an alias, so it doesn't know what service_performance.Service_ID is.
Secondly, in this context the subquery needs to return a single value rather than a table, so you may not be able to reference it. If adding the alias to the subquery doesn't work, then something like the following should work:
update _services
set MAX_VALUE = (SELECT MAX(AVERAGE_MEMORY) AS SERVICE_ID FROM service_performance
INNER JOIN
services
on services.SERVICE_ID = service_performance.SERVICE_ID)
I have three table one is for users and other one is for subject and third one contain user_id, subject_id foreign keys.
I get unknow coloumn when I run the following sql.
SELECT wp_cons_users.first_name, wp_cons_subject.subject, wp_cons_skilllist.skill_level
FROM `wp_cons_subject`
JOIN wp_cons_skilllist ON wp_cons_skilllist.user_id = wp_cons_users.id
JOIN wp_cons_users ON wp_cons_users.id = wp_cons_skilllist.user_id
WHERE wp_cons_subject.id = '1'
ORDER BY `wp_cons_skilllist`.`skill_level` DESC
I can't find the error with this query.
wp_cons_skilllist
column link to
id (primay)
user_id wp_cons_users -> id
subj_id wp_cons_subject -> id
skill_level
Here I try to get the username, skill level and subject for any given subject id.
Looks like your main problem is with the ordering of your JOINs. In your first join, you are matching with wp_cons_users.id, but you don't join that table until later in the query. If you re-order the joins it should work better. Also, based on your table description, it seems that you will also need to join on subject_id. This query should help:
SELECT wp_cons_users.first_name
, wp_cons_subject.subject
, wp_cons_skilllist.skill_level
FROM wp_cons_users
JOIN `wp_cons_subject`
ON wp_cons_users.id=`wp_cons_subject`.user_id
AND wp_cons_subject.id = '1'
JOIN
wp_cons_skilllist
ON wp_cons_skilllist.user_id = wp_cons_users.id
AND wp_cons_skilllist.subject_id = `wp_cons_subject`.id
ORDER BY `wp_cons_skilllist`.`skill_level` DESC
I am guessing about the field names that weren't in your original query, so you may have to make some changes if they're different from what I'm assuming.
Without information about your attributes in your table, I'm afraid we can only assume that there is no ID column in your wp_cons_users table.
when I corrected the query to following it started to work.
SELECT wp_cons_users.first_name, wp_cons_subject.subject, wp_cons_skilllist.skill_level
FROM `wp_cons_skilllist`
JOIN wp_cons_subject ON wp_cons_subject.id = wp_cons_skilllist.subject_id
JOIN wp_cons_users ON wp_cons_users.id = wp_cons_skilllist.user_id
WHERE wp_cons_skilllist.subject_id = '1'
ORDER BY `wp_cons_skilllist`.`skill_level` DESC
LIMIT 0 , 30
I am running into some trouble with the following circumstances:
I have a query that creates two temp tables, and the following select to join them together--
SELECT * FROM result
INNER JOIN result2 ON result2.packetDetailsId = result.packetDetailsId
I am then trying to create another column from concatenating a few of the resulting fields and then use that to reference/query against another table. Is there a way to accomplish this in one query? Should I get away from the temp tables?
Thank you again in advance.
update: If I try to alias the combination of the two temp tables I get an error message stating [Err] 1060 - Duplicate column name 'packetDetailsId'
select * from (
SELECT * FROM result
INNER JOIN result2 ON result2.packetDetailsId = result.packetDetailsId) as myalias
Another Update: I almost have it working as one query but I get the result "(BLOB)" in the column I concoctenated:
select packet_details.packetDetailsId,products.productId,Credit,AccountNum,OrderStat, CONCAT(products.productId,Credit,'_',OrderStat) as consol from (
select packetDetailsId, GROUP_CONCAT(Credit) AS Credit, GROUP_CONCAT(AccountNum) AS AccountNum, GROUP_CONCAT(OrderStat) AS OrderStat FROM
( SELECT pd_extrafields.packetDetailsId,
CASE WHEN pd_extrafields.ex_title LIKE ('%Credit%')
THEN pd_extrafields.ex_value ELSE NULL END as Credit,
CASE WHEN pd_extrafields.ex_title LIKE ('%Account%')
THEN pd_extrafields.ex_value ELSE NULL END as AccountNum,
CASE WHEN pd_extrafields.ex_title LIKE ('%Existing%')
THEN pd_extrafields.ex_value ELSE NULL END as OrderStat
FROM pd_extrafields )AS TempTab GROUP BY packetDetailsId ) as alias2
INNER JOIN packet_details ON alias2.packetDetailsId = packet_details.packetDetailsId
INNER JOIN sales ON packet_details.packetDetailsId = sales.packetDetailsId
INNER JOIN sold_products ON sales.saleId = sold_products.saleId
INNER JOIN products ON sold_products.productId = products.productId
If I understand correctly, you already have the temporary tables created and you need to "concatenate" the results, using from ... inner join ...
The only possible restriction you may have is that you can only reference your temporary tables once in your from clause; besides that, there are no other restrictions (I frequently use temporary tables as intermediate steps in the creation of my final result).
Tips
Let's say your temp tables are temp_result1 and temp_result2. Both tables have a field packedDetailsId, on which the join will be performed. Remember to create the appropriate indexes on each table; at the very least you need to index packedDetailsId on both tables:
alter table temp_result1
add index PDI(packedDetailsId);
alter table temp_result2
add index PDI(packedDetailsId);
Now, just execute a query with the desired join and concatenation. If concat returns BLOB, then cast the result as char (of course, I'm assuming you need a text string):
select r1.*, r2.*, cast(concat(r1.field1, ',', r2.field2) as char) as data_concat
from temp_result1 as r1
inner join temp_result2 as r2 on r1.packedDetailsId = r2.packedDetailsId;
I see your problem is that GROUP_CONCAT is returning BLOB values... It's normal (MySQL doesn't know a priori how to return the values, so it returns binary data); just use the cast function.
Hope this helps you
so, if the result2 and result are both temp tables, you will have to include the # if local temp table and ## if global temp table
so your statements should be :
SELECT * FROM #result
INNER JOIN #result2 ON #result2.packetDetailsId = #result.packetDetailsId
My Bad. This is only applicable for MS SQL