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.
Related
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.
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.
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
Like this:
SELECT s.*, count( logs.* ) as ssh_count
FROM servers s
LEFT JOIN logs ON s.ip_address = logs.server_ip
But I get an error with that query:
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 ssh_count FROM servers s LEFT JOIN logs ON s.ip_address = logs.server_ip LIMIT' at line 1
I think that's because you can't address a table in the count function.
I can do this using a subquery, but that will likely slowly down the query.
What is a better way of doing this?
You can adress a table column, but you can't address table.*, you can do this for example:
SELECT s.*, count( logs.server_ip ) as ssh_count
FROM servers s
LEFT JOIN logs ON s.ip_address = logs.server_ip
I have two MySQL tables:
Group(gr_id, gr_name, gr_description, parent_id)
Group_has_User(User_id, Group_id)
I'm trying to execute the query:
SELECT group.gr_id, group.gr_name, group.gr_description, group.parent_id
FROM group, Group_has_User AS gu
WHERE (group.gr_id = gu.Group_id) AND gu.User_id = 1
It gives 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 'group, Group_has_User AS gu WHERE (group.gr_id = gu.Group_id) AND gu.User_id = 1' at line 1
How should I write it correct?
group is a keyword in SQL. Enclose such names in backticks
FROM `group`, Group_has_User AS gu
group is a keyword in SQL. Try giving your tables more sensible names, or using:
SELECT g.gr_id, g.gr_name, g.gr_description, g.parent_id
FROM `group` g, Group_has_User AS gu
WHERE (g.gr_id = gu.Group_id) AND gu.User_id = 1
Try this. Remove the "AS" keyword after the table name Group_has_User and execute the query
Maybe you must write 'Group', not 'group'.