How to findout the grade using two tables in SQL? - mysql

Having two tables as below:
TABLE 1
TABLE 2
I need to findout the Grades of TABLE 1 from table 2 using Mysql

You can join with the >= and <= operators:
SELECT t1.name, t1.marks, t2.grade
FROM t1
JOIN t2 ON t1.marks >= t2.min_mark and t1.marks <= t2.max_mark

Related

mysql group by return all the rows

I have two tables (table 1 and table 2). Table 1 consists the options and table 2 consists the result for each options.
**table1** table2
id id
optionvalue optionvalueid
time (unixtime)
So when the data is inserted it will be stored in table2. There are 5 optionvalue in table 1 and when data is inserted, then in the table2 it will insert the optionvalueid of table 1 and timestamp in unixtimestamp. Eaach month, I want to count the number of values for each optionvalue. Evene if there is no value for an optionvalue, I still want to see count as zero.
I did the following query but only return the value with rows with data only.
SELECT
po.id,po.optionvalue, COUNT(pr.optionvalueid) as votes,
FROM_UNIXTIME(`time`, '%m-%Y') as ndate
FROM table 1
LEFT JOIN table 2 pr ON po.id=pr.optionvalueid
GROUP BY ndate, po.optionvalue ORDER BY ndate ASC
Is there any other ways to make the query so that it will return all the options even if there is no value.
You can CROSS join table1 to the distinct months of table2 and then LEFT join to table2 to aggregate:
SELECT t.ndate, t1.id, t1.optionvalue, COUNT(t2.optionvalueid) votes
FROM table1 t1
CROSS JOIN (SELECT DISTINCT FROM_UNIXTIME(`time`, '%m-%Y') ndate FROM table2) t
LEFT JOIN table2 t2 ON t1.id = t2.optionvalueid AND t.ndate = FROM_UNIXTIME(t2.`time`, '%m-%Y')
GROUP BY t.ndate, t1.id, t1.optionvalue
ORDER BY t.ndate ASC

Select most recent rows from inner joined tables in MySQL

I want to get the most recent row from each inner joined tables. Both tables have a timestamp field. Below is what I have so far. But it only targets for table1 how about for table2?
SELECT
`table1`.`fieldX`,
`table2`.`fieldY`
FROM `db`.`table1`
INNER JOIN `db`.`table2`
ON `table1`.`id` = `table2`.`id`
WHERE `table1`.`id` = ?
ORDER BY `table1`.`timestamp`
DESC LIMIT 1
table1
row_id
id
fieldX
timestamp
table2
row_id
id
fieldY
timestamp
Both tables can have repeating ids. It was designed this way to store older versions of the data entries.
For example: table1 can have 3 rows with the same id while table2 can have 2 rows of the same id. I want to get the latest row from both tables.
Use can use it like this if you want recent record from table2
SELECT SUBSTRING_INDEX(GROUP_CONCAT(t1.fieldX ORDER BY t1.timestamp DESC),',',1) as field_x, SUBSTRING_INDEX(GROUP_CONCAT(t2.fieldY ORDER BY t2.timestamp DESC),',',1) as field_y
FROM table1 t1
JOIN table2 t2 ON(t2.id = t1.id)
GROUP BY t1.id
ORDER BY t1.timestamp

Inner Join 3 tables pl/sql

Basically i want to inner join 3 tables and the case is understood by the query itself as follows.
Tables:
A has 2 columns column 1 and column 2
B has 2 columns column 3 and column 4
C has 3 columns column 5,column 6 and column 7
Query:
select A.column1, C.Count(C.column6)
from table1 as A inner join table3 as C on A.column2 = C.column5
inner join table2 as B on C.column5 = B.column4 and column3 = 'abcd'
where column7 > NOW() - Interval '30 days'
group by C.column5
order by C.count(column5) desc
But I am getting an error schema C does not exist
Why is this happening? Any mistake with the query?
The issue is that you are using the alias C where you should not, with the count function. This:
C.Count(C.column6)
should be:
Count(C.column6)
and the same change applies in the order by clause (which might be counting the wrong column - should it not be column6?):
order by C.count(column5) desc -> order by count(column6) desc
Also: you should reference all non-aggregated columns in the group by clause, so it should probably be group by A.column1

MySQL IS IN() AS A RESULT

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

Selecting dates next to each other in Mysql

Been trying to figure this out for a while now.
I am looking to select rows from a database table in MySQL where the two dates are next to each other.
e.g. 2011-07-20 is next to 2011-07-21.
Many Thanks
Neil
I guess you can JOIN using ADDDATE():
SELECT T1.id, T2.id
FROM myTable AS T1
INNER JOIN myTable AS T2
ON T1.DATE = ADDDATE(T2.DATE, -1)
WHERE T1.id < T2.id;
The WHERE is to verify that T1 and T2 don't contain duplicates.
SELECT * FROM table WHERE date_column BETWEEN '2011-07-20' AND DATE_ADD('2011-07-20', INTERVAL 1 day)
Manual:
between operator
date_add function