I have two tables, which I will call table1 and table2. Table1 has 2 fields, id and auth, table2 also has two fields, id and keywords. Note that id of table1 and table2 match.
This is my query:
SELECT id, MATCH(keywords) AGAINST('example') FROM table2 WHERE MATCH(keywords) AGAINST('example')
How am I going to exclude results where the auth (table1) of that same id is not 1?
SELECT id, MATCH(keywords) AGAINST('example')
FROM table2 t2
WHERE MATCH(keywords) AGAINST('example')
AND NOT EXISTS (select 1 from table1 t1 where t1.id = t2.id and t1.auth != 1)
SELECT *
FROM table1 t1
INNER JOIN table2 t2
ON t1.id != t2.id
Related
I have 2 tables, one is table1
and another is table 2
I want the result by a query, like
I have tried select id from table2 order by (select id from table1); but it is giving error.
You can join and sort. But you need a column that defines the ordering of the rows in table1. Let me assume that you have such column, and that is is called ordering_id.
select t2.*
from table2 t2
inner join table1 t1 on t1.id = t2.id
order by t1.ordering_id
You can even use a subquery in the order by clause:
select *
from table2 t2
order by (select t1.ordering_id from table1 t1 where t1.id = t2.id)
Join the two tables and then order the result.But for that you need to have some column for ordering and this does not seems to be the case. Syntax you are using for ordering will not work.
SELECT A.ID, B.NAME FROM TABLE1 A INNER JOIN TABLE2 B
ON(A.ID = B.ID) ORDER BY A.ID DESC
finally got the answer
select t2.*
from table2 t2
inner join table1 t1 on t1.id = t2.id;
Here are 2 tables.
Table 1
id value
1 3
2 2
3 3
4 1
5 4
6 3
Table 2
id
1
3
4
How do I get the ids that are in Table 2 which have the max value in Table 1?
Output:
id
1
3
I already tried the following to get the max value, but I cannot figure out how to use it in a single query to get the matching rows. Because I think I need to select from the same table I just inner joined.
select max(table1.value)
from table2
inner join table1 on table1.id = table2.id;
Here is one method:
select t2.id
from (select t2.*, rank() over (order by value desc) as seqnum
from table2 t2 join
table1 t1
on t2.id = t1.id
) t
where seqnum = 1;
Or, an alternative that puts all the ids on one row:
select group_concat(t2.id) as ids
from table2 t2 join
table1 t1
on t2.id = t1.id
group by t1.value
order by t1.value desc
limit 1;
You have a couple of options available without using window functions:
You can use a WHERE clause to select only id values that have a value equal to the MAX(value) from your query and an id that is in Table2:
SELECT t1.id
FROM Table1 t1
WHERE value = (
SELECT MAX(t1.value)
FROM Table2 t2
JOIN Table1 t1 ON t1.id = t2.id
)
AND id IN (SELECT id FROM Table2)
You can JOIN your query to Table1 and Table2 again, matching the value in Table1 and the id in Table2:
SELECT t1.id
FROM (
SELECT MAX(t1.value) AS max_value
FROM Table2 t2
JOIN Table1 t1 ON t1.id = t2.id
) t
JOIN Table1 t1 ON t1.value = t.max_value
JOIN Table2 t2 ON t2.id = t1.id
In both cases the output is
id
1
3
Demo on SQLFiddle
Too low to comment but from the SQL statement you gave, you just need to add the tableid in your select parameters.
select table2.id, max(table1.value)
from table2
inner join table1 on table1.id = table2.id;
I need to create a select statement where the statement need to retrieve data from other table column data
eg.
Table1 Table2
id id2
age age2
Select id, age from table 1 where id= id2
Is that possible.
You can use INNER JOIN
SELECT
T1.id,
T1.age
FROM Table1 AS T1
INNER JOIN Table2 AS T2
ON T1.id = T2.id2
DEMO using INNER JOIN
You can use EXISTS
SELECT
T1.id,
T1.age
FROM Table1 AS T1
WHERE EXISTS(
SELECT 1
FROM Table2 AS T2
WHERE T2.id2 = T1.id
);
You can use IN
SELECT
T1.id,
T1.age
FROM Table1 AS T1
WHERE T1.id IN (SELECT T2.id2 FROM Table2 AS T2)
Note:
In the working demo the output consists of two rows. There are two entries in tabel1 and three entries in table2. But there are only two matching entries found between these two tables. That's why output consists of only two rows.
Yes you can. It is called a JOIN and there are several types of JOINs. I suggest you read up on them on SQL JOINs.
SELECT id ,age
FROM TABLE 1
WHERE id IN (SELECT id2 FROM TABLE2);
OR
SELECT id ,age
FROM TABLE1 , TABLE2
WHERE id = id2 ;
OR
SELECT id ,age
FROM TABLE 1 , (SELECT id2 FROM TABLE2) TBL2
WHERE id = TBL2.id2 ;
Query 1:
SELECT if(COUNT(0),1,0) as 'IsPresent'
FROM table1
WHERE Id=1500;
Query2:
If IsPresent is 1, then
select t2.mark,t2.age from table2 t2,table1 t1
where t1.ID=t2.ID order by t1.ID;
If IsPresent is 0, then
select mark,age from table2;
ie. if entry is present in a table, i need to join else i don't need to join.
Is there any way we can achieve this with a single mysql select query?
I think you can union the two different query cases which would look like:
SELECT T2.MARK, T2.AGE
FROM TABLE1 T1, TABLE2 T2
WHERE
T1.ID=T2.ID AND
T1.ID=1500
UNION
SELECT MARK, AGE
FROM TABLE1
WHERE
NOT ID=1500
SELECT t2.mark, t2.age
FROM table2 t2
JOIN table1 t1
ON t1.id = t2.id
WHERE EXISTS
( SELECT *
FROM table1
WHERE id=1500
)
UNION ALL
SELECT t2.mark, t2.age
FROM table2 t2
WHERE NOT EXISTS
( SELECT *
FROM table1
WHERE id=1500
)
which can be simplified to:
SELECT t2.mark, t2.age
FROM table2 t2
LEFT JOIN table1 t1
ON t1.id = t2.id
AND EXISTS
( SELECT *
FROM table1
WHERE id=1500
)
I currently have a query
SELECT id FROM table1 WHERE {filters on table1} AND id NOT IN (SELECT table1ID FROM table2 WHERE condition = 0)
Table1 has a 1 - Many relationship with table2 and I'm looking for all the IDs that have no entries in table2 with condition=0.
Is there any way to rewrite this query without the inner select? I'm been scratching my head about it for a while now and any pointers would be welcome.
You can try something like
SELECT id
FROM table1 t1 LEFT JOIN
table2 t2 ON t1.ID = t2.table1ID
AND t2.Condition = 0
WHERE {filters on table1}
AND t2.table1ID IS NULL
Or just as good would be
SELECT id
FROM table1 t1
WHERE {filters on table1}
AND NOT EXISTS (
SELECT 1
FROM table2 t2
WHERE t1.ID = t2.table1ID
ADN t2.condition = 0
)