Im trying to do a select query on a table along with an inner join afterwards to link data from the owner to the cats
the ownercat is using a foreign key on the id linking to the ownerinfo id
USE CATTERY;
SELECT
OWNERINFO.ID, OWNERINFO.First_Name, OWNERINFO.Last_Name, OWNERINFO.Phone, OWNERINFO.AddrL1, OWNERINFO.AddrL2, OWNERINFO.AddrL3, OWNERINFO.PostCode,
GROUP_CONCAT(DISTINCT OWNERCAT.Chip_ID)
FROM OWNERINFO
INNER JOIN OWNERCAT ON OWNERINFO.ID = OWNERCAT.ID
WHERE ID = 1;
I get returned the following error:
Error Code: 1052. Column 'ID' in where clause is ambiguous 0.0014 sec
removing the concat distinct statement still produces the same error, im not sure how to get around this issue
You need to define from which table the ID on WHERE-clause come from (you can use aliases). Secondly, as you are using GROUP_CONCAT, you should have GROUP BY in the query:
SELECT
oi.ID,
oi.First_Name,
oi.Last_Name,
oi.Phone,
oi.AddrL1,
oi.AddrL2,
oi.AddrL3,
oi.PostCode,
GROUP_CONCAT(DISTINCT oc.Chip_ID)
FROM OWNERINFO oi
INNER JOIN OWNERCAT oc ON oc.ID=oi.ID
WHERE oi.ID = 1
GROUP BY oi.ID
The problem is in the WHERE clause: ID is ambiguous, because that column is available in both tables.
You may think that, since you are joining the tables on ID, the database is able to tell that it has the same value, but that's not actually the case.
So just qualify the column in the WHERE clause, ie change this:
WHERE ID = 1
To either:
WHERE OWNERINFO.ID = 1
Or the equivalent:
WHERE OWNERCAT.ID = 1
Also please note that your query uses GROUP_CONCAT(), which is an aggregate function. This implies that you need a GROUP BY clause, that should list all non-aggregated column (ie all columns other than the one that is within GROUP_CONCAT()).
Related
I have this somewhat complex sql query that works ok without the final where clause. I'm looking to filter some records using the column unreviewed_records which is an alias
Problem is that I get an error saying unreviewed_records cannot be found. I found some information saying that alias fields are not permitted to be used in where clauses and I'm not sure what's the best way to fix this. Considered using a computed column but I'm not sure how that works yet and I'm hoping there's an easier fix to the query.
Also I find that switching to using the "having" clause work for aliases, but I'll only resort to this if there's no better alternative, to avoid the performance hit.
Any pointers would be helpful :)
select
r_alias.serv_id, r_alias.node_id,
SUM(g_alias.total_records)- SUM(r_alias.reviewed_records) AS unreviewed_records,
SUM(r_alias.reviewed_records) AS reviewed_records,
SUM(g_alias.total_records) AS total_records,
FROM (
SELECT prs.serv_id,
prs.node_id,
SUM(prs.reviewed_records) AS reviewed_records,
FROM p_rev_server prs
WHERE
prs.area_id = 3
AND prs.subId = 3
AND prs.sId = 12
GROUP BY prs.serv_id, prs.node_id, prs.domain_name
) r_alias
INNER JOIN (SELECT
serv_id,
node_id,
SUM(pgs.total_records) AS total_records,
FROM p_gen_serve pgs
WHERE pgs.area_id = 3
AND pgs.subId = 3
AND pgs.sId = 12
AND pgs.total_records > 0
GROUP BY pgs.serv_id, pgs.node_id, pgs.domain_name
) g_alias
ON g_alias.serv_id = r_alias.serv_id AND g_alias.node_id = r_alias.node_id
LEFT JOIN p_cust_columns cust_cols
ON cust_cols.node_id = r_alias.node_id AND cust_cols.serv_id = r_alias.serv_id
where (((NOT (unreviewed_records IS NULL)) AND (unreviewed_records = 5)))
group by r_alias.serv_id, r_alias.node_id
order by g_alias.node_id ASC
limit 25
The reason aliases are not allowed in a WHERE clause is that the expressions in the SELECT list are not evaluated until after the rows are filtered by the WHERE clause. So it's a chicken-and-egg problem.
The easiest and most common alternative is a derived table:
SELECT a, b, c
FROM (
SELECT a, b, a+b AS c
FROM mytable
WHERE b = 1234
) AS t
WHERE c = 42;
This example shows that you can put some filtering conditions inside the derived table subquery, so you can at least reduce the result set partially, before the result of the subquery is turned into a temporary table.
Then in the outer query, you can reference a column that was derived from an expression in the select-list of the subquery. In this example, it's the c column.
The CTE approach is basically the same, it creates a temporary table to store the result of the inner query (the CTE), and then you can apply conditions to that in the outer query.
WITH t AS (
SELECT a, b, a+b AS c
FROM mytable
WHERE b = 1234
)
SELECT a, b, c
FROM t
WHERE c = 42;
The CTE solution is not better than the derived-table approach, unless you need to reference the CTE multiple times in the outer query, i.e. doing a self-join.
Yeah, you are kind of SOL, WHERE can't know what an alias will be. So, frankly, a CTE, common table expression, is probably your best bet here. It should work, though not all RDBMS really support them (MySQL for example only in version 8).
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
Good Day
I have the following query but I'm getting an error message 'Operand should contain 1 column(s)'
Any assistance would be greatly appreciated
UPDATE expenditure
SET BP = (
SELECT * ,
SUM(balance_provision - actual_amt_voucher) over (partition by voteid order by expenditureid) AS BalanceProvision
FROM expenditure
)
It looks like you want to update column bp with a window sum.
Your query fails because you are trying to assign a resultset (that has multiple columns) to a single column.
But even you were returning a scalar value from the subquery, this would not work, since MySQL does not allow reusing the target table of the update in a subquery.
Instead, yo can use the update ... join syntax. Assuming that expenditureid is the primary key of the table, as its name suggests, that would be:
update expenditure e
inner join (
select
expenditureid,
sum(balance_provision - actual_amt_voucher) over (partition by voteid order by expenditureid) bp
from expenditure
group by expenditureid
) e1 on e.expenditureid = e1.expenditureid
set e.bp = e1.bp
I need to execute the below Query but not able to do it.
SELECT *
FROM (SELECT *
FROM rider_status
order by created_date DESC) as riderActice
INNER JOIN rider_user ON rider_userId = rider_user.id
Where rider_user.isActive = 1
AND online_status = 1
GROUP by rider_userId
Error: "#1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'riderActice.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by"
I have read some blogs and found the below solution.
SET GLOBAL sql_mode=(SELECT REPLACE(##sql_mode,'ONLY_FULL_GROUP_BY',''));
But Yet executing above solution I am getting another issue which is
SET GLOBAL sql_mode=(SELECT REPLACE(##sql_mode,'ONLY_FULL_GROUP_BY',''));
Please let me know if I am missing something or doing it wrong
You can not select all(* means all) columns from a table and do a group by one column. That is why you are getting an error. If the column is in the select clause and it is not a part of aggregate function then it has to be in a group by clause.
Also, use the aliases you have created riderActice when joining two tables(subquery and a table).
Here is a small demo demonstrating first part of my answer.
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=b10244e667e59bffb146170014dc69ba
If you want to select all columns then do it like this:
SELECT riderActice.rider_userId
, riderActice.created_date
, rider_user.id
, rider_user.isActive
, rider_user.online_status
FROM rider_status as riderActice
INNER JOIN rider_user ON riderActice.rider_userId = rider_user.id
WHERE rider_user.isActive = 1
AND rider_user.online_status = 1
GROUP BY riderActice.rider_userId
, riderActice.created_date
, rider_user.id
, rider_user.isActive
, rider_user.online_status
If you do not want to group by every column then explain to us what is it that you need, but this is the answer to your question.
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