Select rows where column b is found in column a - mysql

I have a table that looks like this...
------------------------------------
| sku | superseded_sku |
------------------------------------
| PartA | PartC |
| PartB | PartC |
| PartC | PartD |
| PartD | |
I need to write a query that will show me all rows where an entry from coumn B appears in column A. For example here it would give me the following...
------------------------------------
| sku | superseded_sku |
------------------------------------
| PartC | PartD |
| PartD | |
I have tried this...
SELECT x.sku, x.superseded_sku FROM table x
JOIN table y ON y.sku = x.sku
WHERE y.superseded_sku = x.sku
but it returns nothing and now I don't know where to go from here

Think this query will give you the result:
SELECT x.sku, x.superseded_sku FROM table x
JOIN table y ON y.sku = x.superseded_sku

Related

Mysql, How to group a set of rows by using the max value of another column?

I have a table that might have one or more rows with the same value for the key_property column.
I want to build a query in MySql that returns a set where every value of key_property is represented for only one of its corresponding rows and that it also receives a key_property filter for a like sentence, chosen by the max value of another column (say, event_id).
How can I achieve that?
UPDATE:
Here's a sample of the non-filtered table:
+--------------+--------+----------+
| key_property | others | event_id |
+--------------+--------+----------+
| abcd | B | 1 |
| abcd | A | 2 |
| defg | C | 3 |
| abcd | D | 4 |
| hijk | f | 4 |
+--------------+--------+----------+
When executing the query with the filter setted as 'd', the result data should look like this:
+--------------+--------+----------+
| key_property | others | event_id |
+--------------+--------+----------+
| abcd | D | 4 |
| defg | C | 3 |
+--------------+--------+----------+
SELECT *
FROM tab WHERE (key_property,event_id) IN
( SELECT key_property, MAX(event_id)
FROM tab
WHERE key_property like '%d%'
GROUP BY key_property
)

Query needed to get an column values of one table which are not there in string column of another table

I have a two tables :
mysql> select * from quizquestionbank;
| ID | QuestionFilePath | CorrectAnswer |
| 1 | p.wav | 1 |
| 2 | q.wav | 2 |
| 3 | a.wav | 3 |
| 4 | b.wav | 1 |
| 5 | m.wav | 3 |
Second table is :
mysql> select * from quizuserdetails;
| ID | MSISDN | QuestionIdDetails | AnswerRecord |
| 1 | 235346 | 1,3,4,5 | S,F,S,F |
| 2 | 564574 | 4,5,67,88 | F,S,F,s |
| 3 | 500574 | 5,55,66,44,2 | F,F,F,F |
I want to get the IDs from table 1 which are not there in QuestionIdDetails column of second table.
I tried query
Select ID from quizquestionbank where ID not in (Select QuestionIdDetails from quizuserdetails where msisdn = '235346 ');
But this does not work
CAn anybody suggest a way to do it
Use find_in_set() to match the id to the list, but that's not all:
Select disting qb.ID
from quizquestionbank qb
left join quizuserdetails qd
on find_in_set(qb.id, QuestionIdDetails) > 0
and msisdn = '235346'
where qd.id is null
There's 3 key things going on here:
using a left join and including the extra condition in the join condition
the use of find_in_set(), which finds a value in a CSV string, to make the join
using a where clause that filters out matches, leaving only the missed joins

COUNT reducing results?

A query without COUNT returns 3 records, with only 1.
SELECT `blog_cate` . * , COUNT( blogi.blog_cate ) AS num
FROM (
`blog_cate`
)
JOIN `blogi` ON `blogi`.`blog_cate` = `blog_cate`.`blogi_cate_url`
results:
+----+------------------+----------------+-----+
| id | blogi_cate_title | blogi_cate_url | num |
+----+------------------+----------------+-----+
| 1 | Базы данных | batabase | 3 |
+----+------------------+----------------+-----+
And the same query, but without a COUNT:
SELECT `blog_cate` . *
FROM (
`blog_cate`
)
JOIN `blogi` ON `blogi`.`blog_cate` = `blog_cate`.`blogi_cate_url`
That returns me 3 records:
+----+------------------+----------------+
| id | blogi_cate_title | blogi_cate_url |
+----+------------------+----------------+
| 1 | Базы данных | batabase |
| 1 | Базы данных | batabase |
| 3 | Разработка | razrabotka |
+----+------------------+----------------+
Is it possible to use a COUNT and have a normal results?
p.s. tables:
+----+------------+
| id | blog_cate |
+----+------------+
| 1 | batabase |
| 2 | batabase |
| 3 | razrabotka |
+----+------------+
+----+------------------+----------------+
| id | blogi_cate_title | blogi_cate_url |
+----+------------------+----------------+
| 1 | Базы данных | batabase |
| 2 | PHP | php |
| 3 | Разработка | razrabotka |
+----+------------------+----------------+
COUNT() with out a group by will group all records and produce a count of them. Adding more fields to the select will only show the details of the first record
You could build one query to get the three rows and one query to get the count result and join them via cross join to combine every detail row with the count row.

MySQL, get a sum from column after grouped by another column

I have a DB table votes that looks like
| id | val | against_id | created_at |
-----------------------------------------------------
A | 1 | B | xxx
B | -1 | A | xxx
C | 1 | B | xxx
B | -1 | C | xxx
...
For each "vote" there are 2 rows.
I want to get a result that lists every id distinctly with the sum() of val, like:
| id | score |
-----------------
A | 42 |
B | 5 |
C | -15 |
D | 150 |
Is there an equivalent of foreach in mysql?
Your question isn't that clear but have you tried a simple, SUM/GROUP BY?
SELECT ID, SUM(Score) FROM MyTable GROUP BY ID

Mysql join producing huge result table for small solution set

I have two tables where I'm trying to select rows where a common field between those tables matches exactly, however it's proving difficult to write the query. Here is a simplified version:
The tables look like this (simplified):
T1:
id, name, sn
T2:
id, location, sn
I'm trying to get t1.name and t2.loc together only where t1.sn=t2.sn. The sn field is unique in both, and so at the most, only 1 record will match between tables. In t1, all records have a sn field value, however in t2 about 30% of them have NULL for sn. So, I an expecting the join to produce somewhat fewer rows than t1 has.
How would I do the join?
Thanks.
Sample data:
t1:
+---+--------+-------+-----+
| id| name | sn | ... |
+---+--------+-------+-----+
| 1 | thing1 | 12345 | |
| 2 | thing2 | 10000 | |
| 3 | thing3 | 33445 | |
| 4 | thing4 | 99223 | |
+---+--------+-------+-----+
T2:
+----+--------+-------+-----+
| id | loc | sn | ... |
+----+--------+-------+-----+
| 90 | here | 12345 | |
| 92 | there | NULL | |
| 96 | near | 33445 | |
| 99 | far | 99223 | |
+----+--------+-------+-----+
Result:
+--------+-------+-------+
| name | loc | sn |
+--------+-------+-------+
| thing1 | here | 12345 |
| thing3 | near | 33445 |
| thing4 | far | 99223 |
+--------+-------+-------+
SELECT
t1.name AS name,
t2.loc AS loc,
t1.sn AS sn
FROM t1
INNER JOIN t2 ON t1.sn=t2.sn