use matched argument from IN() function - mysql

im using the IN() function to match againts some ids.
SELECT * FROM my_table WHERE id IN(id1,id2,id3)
the thing is, now i need to calculate a SUM() based on the matched id, and i would like to do it on the same query. Something like
SELECT *,(SELECT SUM() WHERE id = the_matched_id) FROM my_table WHERE id IN(id1,id2,id3)
¿is it possible? maybe i should consider to change my query, or do it separately. ¿ What a do you suggest?
Thanks!

The matched ID is just the ID of each row from the outer table. You can use different aliases to compare these IDs.
SELECT *, (SELECT SUM(summableColName) FROM my_table t2 WHERE t2.id = t1.id)
FROM my_table t1 WHERE id IN (id1, id2, id3)

Try something like
SELECT id,sum(field) FROM my_table WHERE id IN(id1,id2,id3)
GROUP BY id

Related

Create a new variable in SQL by groupby

I have 2 sql table as follows:
First table t1:
Second table t2:
I need to calculate the count of "Number" column based on "Name" column from t1 and merge it with t2.
I wrote following code. But it seems not working
select *
from (
select Name, count(Number) as count
from t1
group by Name ) as a
join ( select *
from t2 ) as b
on a.Name = b.Name;
Can any one figure out what is wrong ? Thank you very much
I think you want to use SUM() instead of COUNT().
Because SUM() sums some integers, while COUNT() counts number of occurencies.
And as also stated in the comments, multiple columns with same names will create conflicts, so you have to select the wanted columns explicit (that is usually a good idea anyway).
You could obtain your wanted endgoal by this query:
select
SUM(Number),
t1.Name,
(select val1 FROM t2 WHERE t2.Name = t1.Name LIMIT 1) as val1
FROM t1
GROUP BY t1.Name
Example in sqlfiddle: http://sqlfiddle.com/#!9/04dddf/7

Using the result of one query inside another query (MySQL)

I need to get a subset of one of my tables, and then use these id's in another query, is this possible?
Ideally, I need to use the result of this query:
SELECT id
FROM table
GROUP BY col1, co12
HAVING COUNT(*) > 1
inside this query:
UPDATE table
SET col1 = CONCAT(col1, '_1')
WHERE id IN (ABOVE_QUERY)
I think you are looking for something like this:
UPDATE
table INNER JOIN (SELECT MAX(id) as m_id
FROM table
GROUP BY col1, co12
HAVING COUNT(*) > 1) t1
ON table.id = t1.m_id
SET col1 = CONCAT(col1, '_1')
In MySQL you need to use a JOIN because you aren't allowed to update a table referenced in a subquery. And you probably need to use an aggregated function on the ID returned by your subquery.

Use GROUP_CONCAT result in IN Clause Mysql

I have a Query which returns comma separated integers
like :
select GROUP_CONCAT(ids) from table2
now I want to use that result in another query
like :
select * from table1 where column in (select GROUP_CONCAT(ids) from table2)
in this case it will consider only first value in IN clause.
I agree with #Alma that this can't be done with IN, you might be able to do it with FIND_IN_SET, but if you can do it with IN it's probably a better approach :
SELECT *
FROM table1
WHERE find_in_set(ids, (
SELECT GROUP_CONCAT(ids)
FROM table2
)) != 0;
sqlfiddle demo
Any special reason not using a join and using a sub query
select * from table1 t1
JOIN table2 t2 on (t2.column = t1.ids)

Select Pairs in MySQL

Have a table where certain rows come in couples which have a matching GUID. Just wondering how to SELECT all data from the table but ONLY if the rows exist as a couple with a matching GUID.
You can use a query like this:
SELECT *
FROM yourtable
WHERE GUID IN (SELECT GUID FROM yourtable GROUP BY GUID HAVING COUNT(*)=2)
The subquery will return all GUIDs that appears exactly twice, the outer query will return all rows associated to those GUIDs.
Please see fiddle here.
Try something like this:
SELECT t1.*
FROM
table t1
, table t2
WHERE
t1.guid = t2.guid
AND t1.id <> t2.id
;
table: your table name
id: some field that you know is different for both rows
Try
SELECT t.*
FROM Table1 t JOIN
(
SELECT guid
FROM Table1
GROUP BY guid
HAVING COUNT(*) = 2
) q ON t.guid = q.guid
Here is SQLFiddle demo

INSERT SELECT in MySQL with superfluous aggregate column

I'd like to do an insert select where the select statement has aggregate columns for use by a "HAVING" clause, but where I do not actually want those columns to be inserted. A simple example:
INSERT INTO table1 ( a )
SELECT a, MAX (b) AS maxb FROM table2
GROUP BY a
HAVING maxb = 1
Of course, this won't work because there are a different number of columns in the INSERT and the SELECT. Is there as simple way to make this work? I was hoping I could define some sort of null column in the INSERT field list, or something. I was hoping to avoid a subquery in my SELECT statement, although I could probably do it that way if necessary.
INSERT INTO table1 ( a )
SELECT a FROM (SELECT a, MAX (b) AS maxb FROM table2
GROUP BY a
HAVING maxb = 1) t
You can rewrite the query like this
INSERT INTO table1 ( a )
SELECT a FROM table2
GROUP BY a
HAVING MAX (b) = 1