MySQL how to compare list of strings with subquery - mysql

I am trying to write a query that can match a list of strings on a subquery.
Example
TableA
id
value
1
somevalue1
2
somevalue2
TableB
id
value
tableA_id
1
test1
1
2
test2
1
3
test1
2
I need a query that returns all the entries from TableA who have an entry in TableB for a list of strings.
For:
select * from TableA ta where ('test1', 'test2') = (select tb.value from TableB tb where tb.tableA_id = ta.id);
Expected result would be
id
value
1
somevalue1
because this is the only entry in TableA that has entries for both those string values in TableB.
I tried to look on the internet on how to match a list of string in MySQL but didn't found something that I can use, my sql skills are at beginner level.
Thanks in advance.

Actually you may not even need to involve TableA in this query. I suggest the following canonical aggregation approach on TableB:
SELECT tableA_id
FROM TableB
WHERE value IN ('test1', 'test2')
GROUP BY tableA_id
HAVING COUNT(DISTINCT value) = 2;

Related

select 2 tables / multiple columns

I have this following query
SELECT *
FROM tablea,
tableb
WHERE tablea.a = tableb.b
AND partner_id = 1
okay, this works fine, but I want SELECT from multiple columns, for example:
SELECT *
FROM tablea,
tableb
WHERE tablea.a = tableb.b OR tableb.c OR tableb.d OR tableb.e OR...
AND partner_id = 1
but this return with zero
ohh, it's look like funny, but what's the problem?
thank you for your help!
Your current statement is being parsed as:
WHERE (tablea.a = tableb.b) OR (tableb.c) OR (tableb.d) OR (tableb.e) OR...
You must either explicitly compare against tablea.a in each clause or else use MySQL's IN() operator:
WHERE tablea.a IN (tableb.b, tableb.c, tableb.d, tableb.e, ...)

Check for matching values across 2 fields

I am trying to think of a query that will search a table for matching values across 2 fields.
For example, what would be the query to identity tbl_id 202 and tbl_id 203 as having matching values in both tbl_row and tbl_col?
Thanks
tatty27
This isn't the cleanest way to do it, as it'll double-up on the number of rows returned, but it'll show you the dupes. Assuming the table name is tbl:
select t1., t2. from tbl t1, tbl t2 where t1.tbl_row = t2.tbl_row and t1.tbl_col = t2.tbl_col;
select distinct t1.tbl_id
from
tbl as t1
inner join tbl as t2
on t1.tbl_row = t2.tbl_row
and t1.tbl_col = t2.tbl_col
and t1.tbl_id <> t2.tbl_id
Select T1.tbl_id, T2.tbl_id
FROM Table T1, Table T2
WHERE T1.tbl_row = T2.tbl_row and T1.tbl_col = T2.tbl_col and T1.tbl_id <> T2.tbl_id

Check if multiple records match a set of values

Is there a way to write a single query to check if a set of rows matches a set of values? I have one row per set of values that I need to match and I'd like to know if all rows are matched or not. I could perform this via multiple queries such as:
select * from tableName where (value1, value2) = ('someValue1', 'someValue2')
select * from tableName where (value1, value2) = ('someOtherValue1', 'someOtherValue2')
...and so on, up to an arbitrary number of queries. How could this sort of thing be re-written as a single query where the query returns ONLY if all values are matched?
You could try something like:
select t.*
from tableName t
join (select 'someValue1' value1, 'someValue2' value2 union all
select 'someOtherValue1', 'someOtherValue2') v
on t.value1 = v.value1 and t.value2 = v.value2
where 2=
(select count(distinct concat(v1.value1, v1.value2))
from (select 'someValue1' value1, 'someValue2' value2 union all
select 'someOtherValue1', 'someOtherValue2') v1
join tableName t1
on t1.value1 = v1.value1 and t1.value2 = v1.value2)
If you have a large number of value pairs that you want to check, it may be easier to insert them into a temporary table and use the temporary table in the above query, instead of two separate hard-coded virtual tables.
What about:
SELECT *
FROM tableName
WHERE value1 IN ('someValue1', 'someOtherValue1') AND
value2 IN ('someValue2', 'someOtherValue2')
Match if exactly two records found
Select students who got q13 wrong and Q14 right
SELECT qa.StudentID FROM questionAnswer qa, Student s
WHERE qa.StudentID=s.StudentID AND
((QuestionID=13 AND Pass=0) OR (QuestionID=14 AND Pass=1))
GROUP BY qa.StudentID
HAVING COUNT(*)=2;
The Where clause matches any records where q14 is correct and q13 is incorrect
We then group by the StudentID
The having requires there to be two records

A way to SELECT fields from one table and INSERT to another?

I have two tables, TableA and TableB
TableA has 9 fields
TableB has 7 fields
There are 2 fields (id and name) that are identical in both tables, is there a way to select ONLY these two fields from TableA and insert them into TableB?
I have looked at the INSERT INTO... SELECT method using this statement:
INSERT INTO TableB
SELECT id, name
FROM TableA
WHERE id = 1
But I get the following error:
#1136 - Column count doesn't match value count at row 1
I assume this error is not allowing me to insert only 2 fields into the table? If so, is there a way around this or an alternative method?
Thanks
Try:
INSERT INTO TableB(id, name)
SELECT id, name FROM TableA where id = 1;
One would have to assume that the column names in TableB match TableA otherwise you would need to put in the right names.
You need to specify the column names for TableB (and possibly specify TableA.id in the WHERE clause):
INSERT INTO TableB (id, name)
SELECT (id, name)
FROM TableA
WHERE TableA.id = 1
Specify the columns in table b
INSERT INTO TableB (id, name)
SELECT id, name
FROM TableA
WHERE id = 1

union two tables and order by a common field like name which is present in both the tables

I have two tables a and b which has a field name in it.
I need to list the data from these two tables. I thought of using union but in the result list data from the first table appears and then followed by the second.
what i want is to order by the field name so the result should be a mixed up of two tables in the order of name that is order by name.
select slug, name, 1 as mt
from tablea
union
select slug, name, 0 as mt
from tableb
order
by name;
The above is working well for me. will there be any complications in the result of this?
Suppose your query is
SELECT field1 FROM TABLE1 WHERE 1
UNION SELECT field1 FROM TABLE2 WHERE 1
u can make it a subquery like this
SELECT * FROM (SELECT field1 FROM TABLE1 WHERE 1
UNION SELECT field1 FROM TABLE2 WHERE 1) AS `result` ORDER BY `result`.`field1`
Or, you could use a Join query such as:
SELECT tablea.firstname, tablea.middlename, tablea.lastname, tableb.phone
FROM tablea, tableb
WHERE tablea.ID = tableb.ID
Then, you could sort the result however you like.