I have simple sql:
SELECT *
FROM `oc_artists`
WHERE `oc_artists`.`artist_id`=`oc_artists_tags`.`artist_id`
AND `oc_artists_tags`.`artist_tag` LIKE '%klass%'
When I run this I got:
1054 - Unknown column 'oc_artists_tags.artist_id' in 'where clause'
This is a sql for a search script. I need simple return unique results from oc_artists if query matches with oc_artists_tags.artist_tag.
You need to JOIN the table oc_artists_tags too and you can achieve this two way,
Option 1
SELECT *
FROM `oc_artists`
INNER JOIN `test2` on `oc_artists`.`artist_id`=`oc_artists_tags`.`artist_id`
AND `oc_artists_tags`.`artist_tag` LIKE '%klass%'
Option 2
SELECT *
FROM `oc_artists`,`oc_artists_tags`
WHERE `oc_artists`.`artist_id`=`oc_artists_tags`.`artist_id`
AND `oc_artists_tags`.`artist_tag` LIKE '%klass%'
2nd join table is missing from your query, so include oc_artists_tags table in your join...
Finally your query should be-
SELECT *
FROM `oc_artists`, `oc_artists_tags`
WHERE `oc_artists`.`artist_id`=`oc_artists_tags`.`artist_id`
AND `oc_artists_tags`.`artist_tag` LIKE '%klass%'
You can also use join or inner join instead of comma join-
SELECT *
FROM `oc_artists` as oa
join `oc_artists_tags` as oat on oa.artist_id=oat.artist_id
WHERE oat.artist_tag LIKE '%klass%';
To gain performance follow below points-
You should select only required columns instead of *.
join fields must be indexed and better will be that these fields should be integer type.
If possible avoid '%'in left side in like clause as it will not use index and slow your query. for example artist_tag like 'klass%' will use index but '%klass%' will not.
You have to join the other table.Join helps you.
Related
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()).
Is it possible to have a query with two NOT IN on two subqueries:
SELECT u.feedbackid
FROM user_feedback u
WHERE u.feedbackid NOT IN ( SELECT feedbackid
FROM user_feedback_sent)
AND NOT IN (SELECT feedbackid
FROM user_feedback_received)
The query throws an error on the second NOT IN saying incorrect syntax.
You're missing the column name which shuold NOT be in the second subquery. This will probably works as you need:
SELECT u.feedbackid
FROM user_feedback u
WHERE u.feedbackid NOT IN (SELECT a.feedbackid
FROM user_feedback_sent a)
AND u.feedbackid NOT IN (SELECT b.feedbackid
FROM user_feedback_received b)
Identation its always a good practice to implement when writing SQL code.
Hope it helps
I was trying to execute this statement to delete records from the F30026 table that followed the rules listed.. I'm able to run a select * from and a select count(*) from with that statement, but when running it with a delete it doesn't like it.. it gets lost on the 'a' that is to define F30026 as table a
delete from CRPDTA.F30026 a
where exists (
select b.IMLITM from CRPDTA.F4101 b
where a.IELITM=b.IMLITM
and substring(b.IMGLPT,1,2) not in ('FG','IN','RM'));
Thanks!
This looks like an inner join to me, see MySQL - DELETE Syntax
delete a from CRPDTA.F30026 as a
inner join CRPDTA.F4101 as b on a.IELITM = b.IMLITM
where substring(b.IMGLPT, 1, 2) not in ('FG', 'IN', 'RM')
Please note the alias syntax as a and as b.
Instead of the 'exists' function, you can match the id (like you do in the where clause):
delete from CRPDTA.F30026 a
where a.IELITM IN (
select b.IMLITM from CRPDTA.F4101 b
where a.IELITM=b.IMLITM
and substring(b.IMGLPT,1,2) not in ('FG','IN','RM'));
I believe this is what you really want, all IELITMs which meet your criteria.
delete from CRPDTA.F30026
where IELITM IN (
select IMLITM from CRPDTA.F4101
where substring(IMGLPT,1,2) not in ('FG','IN','RM'));
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
i have this query and i get error in mysql
Error Code: 1054. Unknown column 'calc_diff_free' in 'field list'
I understand the problem, but how can i use this column without changing too much
I filtered the query to hide some data and to help you guys to read the query better
insert into ranking_1 (difference_free)
(
select
f.ranking, rc.ranking,
(-1*(f.ranking-rc.ranking)) as calc_diff_free
from base_testing.ranking_temp f
left join ranking_1 rc
on f.id=rc.id
where 1
)
on duplicate key update difference_free=calc_diff_free
Thanks for your help!!
The on duplicate key update clause in your query is outside the scope of the select item where the column calc_diff_free is declared.
You may need to recast your query to put it in scope.
try something like this,
insert into ranking_1 (difference_free)
select -1 * (f.ranking-rc.ranking) as calc_diff_free
from base_testing.ranking_temp f
left join ranking_1 rc
on f.id=rc.id
on duplicate key update difference_free = calc_diff_free
Point: you must have equal number of columns in your insert and select clause.
I think i manage to get it working...
insert into ranking_1 (difference_free)
(
select
f.ranking, rc.ranking,
(-1 * (f.ranking-rc.ranking)) calc_diff_free
from base_testing.ranking_temp f
left join ranking_1 rc
on f.id=rc.id
where 1
)
on duplicate key update difference_free=-1*(f.ranking-rc.ranking)
But it's give me a few warnings...
85672 row(s) affected,Records: 42883, Duplicates: 42789, Warnings: 4