MySQL IS IN() AS A RESULT - mysql

How do I display in SELECT results 1 if a variable exits in other table and 0 if not? Is it possible or I have to JOIN? And in case it is possible only by JOIN, what if my SELECT is really complicated and I want to LIMIT it before JOINING?
Lets say that table 1 and table 2 contain column named pid. Would like to select * from table 1 , limit it to 100 results (limit 100), and add one column to results determinating if a pid of a result in table 1 is in table 2.

Try
SELECT t1.*, (t2.pid IS NOT NULL) exists_in_table2
FROM
(
SELECT *
FROM table1
ORDER BY pid
LIMIT 100
) t1 LEFT JOIN table2 t2
ON t1.pid = t2.pid
Here is SQLFiddle demo

Try #peterm solution and if you want to check more conditions you can use CASE statement
SELECT t1.*,
case when t2.pid IS NULL then 0 else 1 end as exists_in_table2
FROM
(
SELECT *
FROM table1
ORDER BY pid
LIMIT 100
) t1 LEFT JOIN table2 t2
ON t1.pid = t2.pid
This is the same as #peterm told, I just changed the checking with CASE statement and the SQLFIDDLE

Related

SQL query to get 2 values from table 1 and join all possible option from table 2

I want to get value from table1 and join all matching value from table2. The table1 has to be limited to 2 rows, but expecting output should own all matching values for those two ids.
How can I achieve this?
You would use a subquery:
select t1.*, t2.*
from (select t1.*
from table1 t1
limit 10
) t1 left join
table2 t2
on t1.id = t2.table1_id;
Note: This returns two arbitrary rows. Normally, you would have an order by to better specify the rows. And use order by rand() for random rows.
if you want all the value in join for only two row of table 1 you can use a subqiuery with limit 2
select b.id, a.value, b.value2, b.table1_ID
from (
select * from table1
limit 2
) a
inner join table2 on aid = b.table1_ID

Have a left join where duplicates in the second table is involved - MYSQL

Table 1:
user score
------------
A 1
B 2
Table 2:
user comment time
----------------------------
A good <timestamp 1>
A bad <timestamp 2>
B average <timestamp 3>
I want to join these two tables such that I get the below:
user score comment
-------------------------
A 1 good
B 2 average
As you can see I'll need to join the second table's comment based on the timestamp (the most recent timestamp). I tried
SELECT st.user as user,st.score,
case when v.comment is null then 'NA' else v.comment end as comment
FROM tale1
left JOIN (select distinct user,comment,max(time) from table2) v ON st.user=v.user
but this doesnt work.
You can join with a correlated subquery that filters on the latest timestamp:
select
t1.*,
t2.comment
from table1 t1
left join table2 t2
on t2.user = t1.user
and t2.time = (
select max(t22.time)
from table2 t22
where t21.user = t1.user
)
Side note: I am unsure that you do need a left join here (your sample data does not demonstrate that).
You only want one column from table2 so I recommend a correlated subquery:
select t1.*,
(select t2.comment
from table2 t2
where t2.user = t1.user
order by t2.time desc
limit 1
) as comment
from table1 t1;
This query will make optimal use of an index on table2(user, time desc, comment) -- alas, though, I think the desc is ignored in MySQL.

why the sql correct and the inner mechanism for run it?

the sql as follows come from mysql document. it is:
SELECT * FROM t1 AS t
WHERE 2 = (SELECT COUNT(*) FROM t1 WHERE t1.id = t.id);
The document say It finds all rows in table t1 containing a value that occurs twice in a given column , and doesnot explain the sql.
t1 and t is the same table, so the
count(*) in subquery == select count(*) from t
, isn't it?
count(*) in subquery == select count(*) from t
is wrong. because in mysql you can't use it like that. so you have to run it like that to get result of same id having two rows.
if you want to get count of same occurrence,
SELECT id, name, count(*) AS all_count FROM t1 GROUP BY id HAVING all_count > 1 ORDER BY all_count DESC
And also you can get values as your query like this as well,
select * from t1 where id in ( select id from t1 group by id having count(*) > 1 )
The query contains a correlated subquery in WHERE clause:
SELECT COUNT(*) FROM t1 WHERE t1.id = t.id
It is called correlated because it is related to the main query via t.id. So, this subquery counts the number of records having an id value that is equal to the current id value of the record returned by the main query.
Thus, predicate
(SELECT COUNT(*) FROM t1 WHERE t1.id = t.id) = 2
evaluates to true for any row with an id value that occurs twice in the table.
SELECT * FROM t1 AS t
WHERE 2 = (SELECT COUNT(*) FROM t1 WHERE t1.id = t.id);
This query goes through each record in t1 and then in the subquery looks into t1 again to see if in this case id is found 2 times (and only 2 times). You can do the same for any other column in t1 (or any table for that matter).
When you would like to see all values that are multiple times in the table, change WHERE 2 = by WHERE 1 <. This will also give you the values that are 3 times, 4 times, etc. in the table.
{
SELECT id,count( * )
FROM
MyTable
group by id
having count( * )>1
}
with this code, you can see the rows which repet more than one,
and you can change this query by yourself
How about using GROUP BY and HAVING:
SELECT id, count(1) as Total FROM MyTable AS t1
GROUP BY t1.id
HAVING Total = 2

Why does group by and sort by give 0 response

The result of this query gives me 0 rows where it should give me the 3 latest rows (grouped by Table1.Name).
Table1 has: "Name", "Timestamp", "Voltage".
Table2 has: "Name", "data".
When I delete "ORDER BY Table1.Timestamp" I do get 3 rows (as expected) but they are the 3 oldest entries in the database where I want the 3 latest.
(I have 3 Name values in Table1 and Table2 that match).
The code:
SELECT * from Table1
INNER JOIN Table2
ON Table1.Name=Table2.Name
GROUP BY Table1.Name
ORDER BY Table1.Timestamp;
You can try to perform a query like this :
SELECT t.*,t2.* from Table1 t
INNER JOIN Table2 t2
ON t.Name=t2.Name
WHERE t.Timestamp = (
SELECT MAX(t3.Timestamp) FROM Table1 t3
WHERE t3.Name = t.Name
)
You could sort and taking the top 3:
SELECT * from Table1
INNER JOIN Table2
ON Table1.Name=Table2.Name
ORDER BY Table1.Timestamp DESC
LIMIT 3
When you are using join two or more tables then don't use * in select query. Use specific column name to avoid column ambiguously in select query , you can also use Table1., Table2. but good way to use specific columns in select query.
SELECT Table1.Name,Table1.Timestamp from Table1
INNER JOIN Table2
ON Table1.Name=Table2.Name
GROUP BY Table1.Name
ORDER BY Table1.Timestamp;

SELECT FROM view1, view2 when data available (JOIN?)

I'm not a SQL guy, and I've just tried every JOIN MySQL offers.
I primarily need data from view1 using WHERE, ORDER BY, LIMIT with offset (the data is incorrect without those conditions.)
view1 is not limited by view2
Data also needs to be pulled from view2, but view2 has its' own WHERE (or whatever's best) condition
view2 may not have corresponding data, but it shouldn't be grabbed without being linked to view1 (NULLs are just fine!)
I'll just give you the two SELECTs that work for me rather than the hashes I've come up with.
For view1:
SELECT * FROM view1
WHERE column1 IS NULL OR column1 = 1
ORDER BY dateColumn DESC LIMIT index1, count1;
(index1 and count1 are INs i use for a stored proc)
For view2:
SELECT * FROM table2
WHERE column1 = ? AND table1_id IN (
SELECT id FROM view1
WHERE column1 IS NULL OR column1 = 1
ORDER BY dateColumn DESC LIMIT index1, count1
)
GROUP BY table1_id
(? is any arbitrary value i put in)
I'm at wits end. I have no idea how to fuse these two.
Specifics
Please note that the IN for view2 is almost identical to the view1 query. Thanks!
You can write a subquery for table2 and add WHERE clause there, also you can add WHERE clause at the ent of the query -
SELECT * FROM table2 t2
JOIN (
SELECT id FROM table1
WHERE column1 IS NULL OR column1 = 1
ORDER BY dateColumn DESC
LIMIT index1, count1
) t1
ON t1.id = t2.table1_id
WHERE
t2.column1 = ?
GROUP BY
t2.table1_id
In this example I used JOIN instead of WHERE IN condition.
How about this query:
Select t1.id
FROM table1 t1
JOIN table2 t2 ON t1.id = t2.table1_id
WHERE t1.column1 IS NULL OR t1.column1 = 1
i think it should help you
Select TableName1.id
FROM TableName1
JOIN TableName2 ON TableName1.id = TableName2.id
WHERE TableName1.column1 IS NULL OR TableName1.column1 = 1