MYSQL | select inverse value between 2 tables - mysql

I would like to SELECT the records from a table that have different values from another one.
Table 1.
+--------+-------+
| userID | tagID |
+--------+-------+
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
+--------+-------+
Table 2.
+---------+-------+
| ChaName | tagID |
+---------+-------+
| Hello | 1 |
| How | 2 |
| Are | 3 |
| You | 4 |
| Today | 5 |
| Guys | 6 |
| ? | 7 |
+---------+-------+
And then it suppose to be
+--------+-------+---------+
| userID | tagID | chaNAME |
+--------+-------+---------+
| 1 | 1 | Hello |
| 1 | 5 | Today |
| 1 | 6 | Guys |
| 1 | 7 | ? |
+--------+-------+---------+
It looks very simple but I can't find the way to solve it.
Thank you for all of your answers <3
PS. btw i've tried to use ' not in ' but it got an error
Unrecognized keyword. (near "not in" at position 92)

To get the output, you need to use an outer join. A left join will give you all the records from left table table2 and only matching records from right table table1. That means you will get null for records in table1 which doesn't exist in table2. You can use this property in where clause to get only your expected records.
Also you cannot decide on userid on records in table2. If you want to hardcode them as 1, then use select 1 as userid, ... in select clause.
SQLFiddle Demo
select t2.tagid,t2.chaname
from
table2 t2 left join table1 t1
on t1.tagid=t2.tagid
where t1.tagid is null

select a.userid,
b.tagid,
b.chaname
from table1 a,table2 b
where b.tagid not in (select tagid from tabel1)

Related

Query for differences

I have two tables:
table1: id, created
table2: id, modi
+------------+----------+
| id | created |
+------------+----------+
| 1 | 18/01/01 |
| 2 | 18/01/01 |
| 3 | 18/01/01 |
| 4 | 18/01/01 |
+------------+----------+
+------------+----------+
| id | modi |
+------------+----------+
| 1 | 18/01/02 |
| 4 | 18/01/02 |
| 1 | 18/01/03 |
| 2 | 18/01/03 |
| 3 | 18/01/04 |
| 2 | 18/01/04 |
| 2 | 18/01/05 |
+------------+----------+
I need a query (MySQL) that prints out how many days takes every user to modify the log, grouped by days. For example, 1 day - 3 users, 2 day - 7 users, etc...
+------------+----------+
| days | num_id |
+------------+----------+
| 1 | 2 |
| 2 | 1 |
| 3 | 1 |
+------------+----------+
I managed to do this:
select datediff(table1.id, table2.modi) as date_diff, count(*) as nums
from table1, table2
where table1.id = table2.id
group by date_diff
The problem is that it is also including the second entry (or more) besides the first one, but I would only want the first one to be included.
Thanks.
you need to use proper column for datediff fucntion
select datediff(table3.modi,table1.created) as date_diff, count(*) as nums
from table1 left join
(
select id,min(modi) as modi from table2 group by id
)
as table3
on table1.id = table3.id
group by date_diff
just add a Join Left in the query. :)

combine information from two tables into one table

I need to combine information from two tables into one table, the following table is
table 1
+----+---------------+---------+
|id_k| name | value |
+----+---------------+---------+
| 1 | enak | 4 |
| 2 | nor | 3 |
+----+---------------+---------+
table 2
+------+------+---------+
| id_d | id_k | feel |
+------+------+---------+
| 1 | 1 | good |
| 2 | 1 | better |
| 3 | 1 | verygood|
+------+------+---------+
result should be
+------+------+-------+------------------------+
| id_d | name | value | feel |
+------+------+-------+------------------------+
| 1 | enak | 4 | good, better, verygood |
| 2 | nor | 3 | |
+------+------+-------+------------------------+
this is my code [not worked]
select k.name, k.value, s.feel
from table1 as k
left join table2 as s on s.id_k=k.id_k
http://sqlfiddle.com/#!9/3a7564/1
SELECT t1.id_k,
t1.`name`,
t1.`value`,
GROUP_CONCAT(t2.feel) AS feel
FROM table1 t1
LEFT JOIN table2 t2
ON t1.id_k = t2.id_k
GROUP BY t1.id_k
You could use the gorup_concat function to concatinate the values from table2 to a coma-delimited string in the result:
SELECT table1.id_k, name, value, GROUP_CONCAT(feel SEPARATOR ', ') AS feel
FROM table1
JOIN table2 ON table1.id_k = table2.id_k
GROUP BY table1.id_k

mysql select with group by returning rows with a preference order

I have the following mysql query result:
+----+------------+-------------+
| id | title | lang |
+----+------------+--------------
| 1 | ola1 | 1 |
| 1 | hello1 | 2 |
| 1 | bonjour1 | 3 |
| 2 | ola2 | 1 |
| 2 | bonjour2 | 3 |
| 3 | hello3 | 2 |
| 4 | bonjour4 | 3 |
+----+------------+-------------+
What I want is a group_by query by id and that gives me for each id the title with a order of preference for lang field. Example:
Result for lang preference order 1, 2, 3:
+----+------------+-------------+
| id | title | lang |
+----+------------+--------------
| 1 | ola1 | 1 |
| 2 | ola2 | 1 |
| 3 | hello3 | 2 |
| 4 | bonjour4 | 3 |
+----+------------+-------------+
Result for lang preference order 3, 2, 1:
+----+------------+-------------+
| id | title | lang |
+----+------------+--------------
| 1 | bonjour1 | 3 |
| 2 | bonjour2 | 3 |
| 3 | hello3 | 2 |
| 4 | bonjour4 | 3 |
+----+------------+-------------+
Thanks!
It is either not possible, or, not with in my SQL skills to execute that in one query. I always end up using a temporary template and two SQL commands for these problems:
(assuming that your table is called Table1 and the temporary table should be called tempTable)
SELECT Table1.id, Min(Table1.lang) AS Min_Of_lang INTO tempTable FROM Table1 GROUP BY Table1.id ORDER BY Table1.id;
SELECT Table1.* FROM tempTable INNER JOIN Table1 ON (tempTable.MinOflang = Table1.lang) AND (tempTable.id = Table1.id);
The first command creates a new table (that overrides the current table if it exists). The second command uses the first table to produce the desired result set.
To change from your first desired results table to the second, use Max instead of min in the first query.
Somebody else may well have a more elegant solution than mine. Also, an extra SQL statement could be added to delete the temporary table.
This is a feature that is not defined in MySQL. The displayed value in a non-aggregated column is undetermined. read more here (MySQL Documentation).
(Standard SQL doesn't allow to include non-aggregated columns when using GROUP BY, I guess this is one of the reasons).
From your description of the what you want to do, you should simple SELECT all rows with the lang you are looking for
SELECT * FROM your_table WHERE lang = 1

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

Select Distinct Set Common to Subset From Join Table

Given a join table for m-2-m relationship between booth and user
+-----------+------------------+
| booth_id | user_id |
+-----------+------------------+
| 1 | 1 |
| 1 | 2 |
| 1 | 5 |
| 1 | 9 |
| 2 | 1 |
| 2 | 2 |
| 2 | 5 |
| 2 | 10 |
| 3 | 1 |
| 3 | 2 |
| 3 | 3 |
| 3 | 4 |
| 3 | 6 |
| 3 | 11 |
+-----------+------------------+
How can I get a distinct set of booth records that are common between a subset of user ids? For example, if I am given user_id values of 1,2,3, I expect the result set to include only booth with id 3 since it is the only common booth in the join table above between all user_id's provided.
I'm hoping I'm missing a keyword in MySQL to accompish this. The furthest I've come so far is using ... user_id = all (1,2,3) but this is always returning an empty result set (I believe I understand why it is though).
The SQL query for this will be:
select booth_id from table1 where [user_id]
in (1,2,3) group by booth_id having count(booth_id) =
(select count(distinct([user_id])) from table1 where [user_id] in (1,2,3))
If this could help you creating the MySQL query.