Syntax for nested queries in mysql - mysql

I have 3 tables:
stocktake
supplier
accessories
I want to select total_inventories,accessory_id,supplier_id,conpany_name,phone_no
this 5 things in this three table
and the total_inventorieshave to <100
and I have to use Nested Query or either standard or correlated sub-query
that is what I have done
SELECT total_inventories,accessory_id,supplier_id,conpany_name,phone_no
FROM stocktake,supplier,accessories
WHERE stocktake.accessory_id=accessory.accessory_id,accessory.supplier_id=supplier.supplier_id,
accessory_id IN (SELECT accessory_id FROM stocktake WHERE total_inventories <100)
but it got an error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'accessory.supplier_id=supplier.supplier_id,
accessory_id in (select accessory_i' at line 3
How can I fix it?

few things to mention in your code
you are using accessories table but using accessory.supplier_id. so you need to use correct the table name.
always use explicit join instead of implicit joins.
always use alias for the tables for more readability.
use exists instead of IN
FROM stocktake s
join accessories a
on s.accessory_id = a.accessory_id
join supplier su
on a.supplier_id = su.supplier_id
WHERE a.accessory_id IN (SELECT accessory_id FROM stocktake WHERE total_inventories < 100)
In your solution you can replace IN with exists as following
WHERE a.accessory_id exists
(
SELECT
accessory_id
FROM stocktake s
WHERE a.accessory_id = s.accessory_id
and total_inventories < 100
)

You cannot use comma in where operator try. You can also refer join operation in sql.
FROM stocktake,supplier,accessories
WHERE stocktake.accessory_id=accessory.accessory_id and accessory.supplier_id=supplier.supplier_id and
accessory_id IN (SELECT accessory_id FROM stocktake WHERE total_inventories <100)
The query can be optimized as:
FROM stocktake,supplier,accessories
WHERE stocktake.accessory_id=accessory.accessory_id and accessory.supplier_id=supplier.supplier_id and
stocktake.total_inventories <100
We would not need subquery, we can directly filter total_inventories values as we have already joined three tables.

Related

My Sql(MySQL said: #1052 - Column 'hswhouse.whname' in field list is ambiguous

create view House as
SELECT hswhouse.whname,hsitems.description,hsitems.numInStock
from hswhouse
inner join hsitems
inner join hswhouse on hswhouse.whnum = hsitems.whouseNum
where hswhouse.whname = "San Diego"
and MySQL said:
#1052 - Column 'hswhouse.whname' in field list is ambiguous
You have table hswhouse twice in the query, without aliases, so it is indeed ambiguous to which of them table name hswhouse refers.
Looking at your query, one of the joins is missing a on clause (which is a syntax error in most databases apart from MySQL), and it really seems like you need table hswhouse just once. Most likely, you want:
create view House as
SELECT h.whname, i.description, i.numInStock
from hswhouse h
inner join hsitems i on h.whnum = i.whouseNum
where h.whname = 'San Diego'

How to resolve wrong syntax near HAVING COUNT DISTINCT?

This it the code I am trying to execute:
SELECT ID_K
FROM koncert,
programi
WHERE koncert.ID_K = programi.ID_K
GROUP BY koncert.ID_K
HAVING COUNT (DISTINCT programi.Salla) = 2
It returns this error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to
your MariaDB server version for the right syntax to use
near 'DISTINCT programi.Salla)=2 LIMIT 0, 25' at line 4.
Tried to change different things but it still won't work .
You should use the count(DISTINCT programi.Salla
) and not count (..) ..remove space between COUNT and (...
SELECT koncert.ID_K
FROM koncert
INNER JOIN programi on koncert.ID_K = programi.ID_K
GROUP BY koncert.ID_K
HAVING COUNT(DISTINCT programi.Salla) = 2
but you need also tablename for avoid ambiguity and use explicit join sintax too
First you should use qualified name for your column, when column name is same in both table
SELECT ID_K FROM
should be
SELECT programi.ID_K FROM
else, you will get ambiguous column error. Otherwise, your query looks fine except removing extra space when calling COUNT (#spencer already mentioned in comment)
Also, it is good practice to join your table using JOIN (or INNER JOIN, LEFT JOIN etc) keyword, which makes your query more clear and readable.

Intersect command is not working

MariaDB [object]> select Protein, count(mirna) from exp2
INTERSECT select Protein, count(mirna) from exp3 group by Protein;
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MariaDB server version for the right
syntax to use near 'select Protein, count(mirna) from exp3 group by
Protein' at line 1.
I have two tables exp2 and exp3, both have many common rows, I want to query from the common data from these two tables.i.e. I want have a common data table of Protein and corresponding count of miRNAs in number.
I am using lampp, how I can resolve this query?
MySQL doesn't offer the INTERSECT operation. You need to use something like a JOIN operation. This example suppresses all rows that don't match the ON condition.
SELECT a.Protein, a.mirnacount
FROM (SELECT Protein, count(mirna) mirnacount from exp2 group by Protein) a
JOIN (SELECT Protein, count(mirna) mirnacount from exp3 group by Protein) b
ON a.Protein = b.Protein AND a.mirnacount = b.mirnacount
You might consider switching to PostgreSQL if a full complement of set operations are needed for your project.
SELECT T1.Protein,count(T1.miRNAID)
FROM exp2 AS T1
INNER JOIN exp3 AS T2
ON T1.Protein = T2.Protein
AND T1.Target_Protein_id= T2.Target_Protein_id
AND T1.miRNAID=T2.miRNAID
GROUP BY T1.Protein
ORDER BY count(T1.miRNAID)
this works perfectly for me.

Delete duplicates for multiple columns in JOIN on same table

I am trying to make a delete from joined same table like this:
DELETE FROM `sp10_seo_url` AS sp1 JOIN
(
SELECT seo_url_pk, COUNT(*) AS maxc
FROM `sp10_seo_url`
GROUP BY seo_url_entity_type, seo_url_entity_id, seo_url_language_fk
HAVING maxc > 1
) AS sp2
ON sp1.seo_url_pk = sp2.seo_url_pk
However I am getting a mysql error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS sp1 JOIN ( SELECT seo_url_pk, COUNT(*) AS maxc FROM `sp10_s' at line 1
And I am not sure at all where the error is. The inner query runs just fine and returns the expected set of results. The "ON" keys are properly named (same since we are talking about the same table).
I guess the idea of the query is pretty clear (clean the table of different rows have the same set of values for the three "group by" columns. Is there another way to do this?
Thanks!
you can "cheat" mysql with a double indirection (as explained here Deleting a row based on the max value):
delete from `sp10_seo_url`
where seo_url_pk in (
select seo_url_pk from (
SELECT seo_url_pk
FROM `sp10_seo_url` sp1,
(
SELECT seo_url_entity_type, seo_url_entity_id, seo_url_language_fk
FROM `sp10_seo_url`
GROUP BY seo_url_entity_type, seo_url_entity_id, seo_url_language_fk
HAVING count(*) > 1
) sp2
where sp1.seo_url_entity_type = sp2.seo_url_entity_type
and sp1.seo_url_entity_id = sp2.seo_url_entity_id
and sp1.seo_url_language_fk = sp2.seo_url_language_fk
) t
);
http://sqlfiddle.com/#!2/899ff5/1

Joining derived tables

EDIT: I am using phpMyAdmin interface, and I have been copy/paste the codes from phpMyAdmin to here. The phpMyAdmin seems to run a "different code" as I run the following code, and generating some error message that are referring to that "different code", causing huge confusion.
** Final edit: It seems Safari is causing this: it run the "different query" when I try to run 2nd query below. Use Firefox instead, and it generate correct results. Thanks for the help and sorry for the confusion. **
I have two tables: newsFeeds, comments, where
** newsFeeds contains column PID, comments contains column FID.**
I want to join rows in two tables with matching PID = FID. My code is:
SELECT * FROM newsFeeds
INNER JOIN
(
SELECT * FROM
comments
)
ON comments.FID = newsFeeds.PID
and the error message is "#1248 - Every derived table must have its own alias".
I then add in AS newtb after ) according to other posts here. And the code is then:
SELECT * FROM newsFeeds
INNER JOIN
(
SELECT * FROM
comments
) AS newtb
ON newtb.FID = newsFeeds.PID
But another error shows up: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INNER JOIN( SELECTFID,COUNT(*) AScount FROMcomments
LIMIT 0, 25' at line 8
I wonder how to correctly do this?
You should correct this by removing the derived table:
SELECT *
FROM tb_1 INNER JOIN
tb_2
ON tb_2.FID = tb_1.PID;
MySQL has a tendency to materialize derived tables, which hurts performance.
The answer to your question, though, is to add a name after the parentheses:
SELECT *
FROM tb_1 INNER JOIN
(SELECT *
FROM tb_2
) t2
ON t2.FID = tb_1.PID;