How to join a table and then table inside table in mysqli? - join

I have a table name
deposite with columns deposite_id, assigncourse_id
and assigncourse table with columns assigncourse_id, std_id, course_id
and two tables std and course
now i want to join assigncourse table from deposite table and then join with std and course table from assigncourse table, with mysqli query.

SELECT *
FROM deposite d
JOIN (SELECT a.assigncourse_id
FROM assigncourse a
JOIN std t ON a.std_id = t.std_id
JOIN course c ON a.course_id = c.course_id
) m ON d.assigncourse_id = m.assigncourse_id;

Related

Rails query to order records by single association row column

Say I have a books table that has id and name columns and a has_many book_ratings association. The book_ratings table has id, rating, rating_date, and book_id columns and belongs_to :book. I'm displaying the books in a table with 3 columns: the books name, the highest rating for the book, and the most recent rating for the book. I want each column sortable and I'm using the Pagy gem for pagination. I want to make a single call to each table to avoid N+1.
I made this query which includes the max_rating and sorts by max_rating:
#pagy, #books = pagy(
Book.joins(:book_ratings)
.select('books.*,MAX(rating) as max_rating')
.order('max_rating')
.group('books.id')
)
With each book record, I can call book.max_rating to display the max_rating in the table. I cannot think of a way to also include the rating with the latest date and sort the records by that rating. I know I can use MAX(rating_date) to get the date of the book_rating with the most recent date, but I need a way to include the rating value of the book_rating with the latest date and also be able to order the book records by those values.
Any ideas?
To get this to work, I ended up using a select method and then chaining two joins methods. Here's what I ended up using:
Book.select('books.*,t2.rating as latest_rating,t4.rating as highest_rating')
.joins(
"INNER JOIN (
SELECT book_ratings.rating, book_ratings.book_id, book_ratings.rating_date
FROM book_ratings
JOIN (
SELECT book_id, MAX(rating_date) as max_date
FROM book_ratings GROUP BY book_id
) t1
ON t1.book_id = book_ratings.book_id
AND t1.max_date = book_ratings.rating_date
) t2
ON t2.book_id = books.id"
)
.joins(
"INNER JOIN (
SELECT book_ratings.rating, book_ratings.book_id
FROM book_ratings
JOIN (
SELECT book_id, MAX(rating) as max_rating
FROM book_ratings
GROUP BY book_id
) t3
ON t3.book_id = book_ratings.book_id
AND t3.max_rating = book_ratings.rating
) t4
ON t4.book_id = books.id"
)
.distinct
Calling .to_sql on that query gives this SQL:
SELECT DISTINCT books.*,t2.rating as latest_rating,t4.rating as highest_rating
FROM "books"
INNER JOIN (
SELECT book_ratings.rating, book_ratings.book_id, book_ratings.date
FROM book_ratings
JOIN (
SELECT book_id, MAX(date) as max_date
FROM book_ratings
GROUP BY book_id
) t1
ON t1.book_id = book_ratings.book_id
AND t1.max_date = book_ratings.date
) t2
ON t2.book_id = books.id
INNER JOIN (
SELECT book_ratings.rating, book_ratings.book_id
FROM book_ratings
JOIN (
SELECT book_id, MAX(rating) as max_rating
FROM book_ratings
GROUP BY book_id
) t3
ON t3.book_id = book_ratings.book_id
AND t3.max_rating = book_ratings.rating
) t4
ON t4.book_id = books.id
When iterating through this collection, latest_rating and highest_rating can be called on each object and provides the value from the rating column for either. It can also be used with .order('latest_rating asc') (or desc) or .order('highest_rating asc') (or desc)

Join Table B and get values of different columns depending on values in a specific column from Table A

I want to join two tables via ID. The first table has all the IDs and a column with the column names from the other table. I did this with a case statement and if worked but I have like 50 differnet column names and more to come so I don't want to use the case statement. What can I do instead? I'm using Snowflake, this is what I have:
Table A:
tableA
Table B:
tableB
create table statements:
CREATE OR REPLACE TABLE
TABLE_A
(id INTEGER,
column_name VARCHAR);
INSERT INTO
test.TABLE_A
(id,column_name)
VALUES
(1,'column_1'),
(1,'column_2'),
(2,'column_2');
---------------------
CREATE OR REPLACE TABLE
TABLE_B
(id INTEGER,
column_1 VARCHAR,
column_2 VARCHAR);
INSERT INTO
TABLE_B
(id,column_1,column_2)
VALUES
(1,'value_1_1','value_1_2'),
(2,'value_2_1','value_2_2');
This is what I did but as I said this is bad for maintainance:
SELECT
a.ID,
a.COLUMN_NAME,
CASE
WHEN a.column_name = 'column_1'
THEN b.column_1
WHEN a.column_name = 'column_2'
THEN b.column_2
ELSE ''
END AS result
FROM
TABLE_A a
JOIN
TABLE_B b
ON
a.id = b.id
This gives me the following result:
result
How can I get this result without using the case statement or having to type the strings into any condition?

How to write the below SQL query in rails 3 ActiveRecord?

select * from
(
SELECT DISTINCT ON (table1.id) table1.*, table3.date_filed as date_filed
FROM
table1 LEFT JOIN table2 ON table2.id = table1.some_id
INNER JOIN table3 ON table2.id = table3.some_id
WHERE
(
status IN('Supervisor Accepted')
)
AND(table3.is_main)
)first_result
ORDER BY date_filed ASC LIMIT 25 OFFSET 0
Is there any way to run main/subset query in the database side through Active::record (Rails 3). I don't want run the first_result(First db query) and the order by on the top of the result(Second db query).
I tried the below:
# First query run
first_result = Table1.select('DISTINCT ON (table1.id) table1.*, table3.date_filed').
joins('LEFT JOIN table2 ON table2.id = table1.some_id'). # I don't want a association here
joins('INNER JOIN table3 ON table2.id = table3.some_id').
where('table3.is_main')
# Second query run, WHICH is UGLY and not working properly
Table1.where(id: first_result.collect(:&id)).
order_by('date_filed ASC')
page(page).
per_page(per_page)

Joining multiple table order

I think I have read somewhere that join in associative and commutative so order of table in join query is irrelevant, is this true?!!
If so then I have a situation here where I have 4 tables like this:
S(s_id, sname, status, city) supplier
J(j_id, jname, city) job/project
P(p_id, pname, color, weight, city) part
SPJ(s_id, p_id, j_id, qnty) supplier - parts - jobs
So my question is are the following joins the same?
S join J join SPJ
S join SPJ join J

Rails - Using SELECT in a WHERE clause

I'm trying to make a Rails model scope based on the following query:
SELECT * FROM tableA a
INNER JOIN tableB b ON a.id = b.id
WHERE a.id = (SELECT MAX(id) FROM tableB WHERE field = a.field)
I want to join rows of tableA with only one of tableB rows (the max one).
Is it possible?
Thank you!
TableA
.joins(:tableB)
.where("a.id = (SELECT MAX(id) FROM tableB WHERE field = a.field)")
You'll need to have an association between the two tables if you want the joins method works

Resources