Sorting through 3 tables MYSQL - php

Hello I am trying to sort through these 3 Tables
I need to create a query that goes through the 'Author' Table,
grabs the author num
Then goes to the 'Wrote' table to find the 'BookCode' from the AuthorNum of the last table
Then to finally go through the Book table to list the title of the book and the first name and last name of the author.
I was thinking of using a join table but am not too solid on my uinderstanding on how it works. Nested select statements was my next guess but I can't get them to go through so many tables.
If anyone could help me that would be fantastic, thank you.

You want to use INNER JOINS to match up the data
SELECT *
FROM authors AS a
INNER JOIN wrote AS w
ON a.AuthorNum = w.AuthorNum
INNER JOIN book AS b
ON w.BookCode = b.BookCode

Please try to use this :
(I named the first table name to first)
Select a.Title as title, w.AuthorFirst as firstName, w.AuthorLast as lastName
From wrote as w
Inner Join author as a
Inner Join first as f
On (Select ww.AuthorNum From WroteTable as ww Order By DESC LIMIT 1) = f.AuthorNum
On f.BookCode = a.BookCode

Related

How do I inner join 2 SQL tables, but only take the first result from the second table?

I'm building a page where u can rent apartments and stuff,
and there are a lot of images of each of these apartments,
so I have made 2 tables. One with all apartments, and one with all images, that i connect to a specific apartment with a foreign key.
I want all the apartments, but i only want 1 image per apartment.
I have tried to do LIMIT 1, but then it limits apartments to 1 as well.
This is my code:
$sql = "SELECT bolig_boliger.id AS bolig_id, titel, areal, rooms,
indflytning, husleje, image
FROM bolig_boliger
INNER JOIN bolig_images ON bolig_boliger.id = bolig_images.bolig_id";
Join with a subquery that returns one row per apartment ID, instead of joining with the whole table.
SELECT b.id, b.titel, b.areal, b.rooms, b.indflytning, b.husleje, i.image
FROM bolig_boliger AS b
JOIN (
SELECT bolig_id, MAX(image) AS image
FROM bolig_images
GROUP BY bolig_id
) AS i ON b.id = i.bolig_id
One simple method s a correlated subquery. Of course, you have not shown the layout of the tables, so I have to speculate on what columns go where and how to identify the latest row. A reasonable guess is:
SELECT b.id AS bolig_id, titel, areal, rooms, indflytning, husleje, image
FROM bolig_boliger b INNER JOIN
bolig_images i
ON b.id = i.bolig_id
WHERE i.id = (SELECT MAX(i2.id) FROM bolig_images i2 WHERE i2.bolig_id = i.bolig_id)

joining 2 tables in msqli

table posts
table users
how would i count posts for specific user logged in. for example when user with id 3 is logged in it should show me 4 posts
I already did it for total posts count:
<?php
$post_query1 = "SELECT count(*) AS total FROM posts ";
$post_result1 = mysqli_query($db, $post_query1);
$post1 = mysqli_fetch_array($post_result1);
?>
Try below example :
select count(*) as total from user as u inner join post as p on p.id_user = u.id_user AND u.id_user = 3
If you want to get only the posts count for the particular user, say user with id = 3, your query should be this:
$query = "SELECT count(*) AS total FROM posts WHERE id_users = 3";
But if you want to get both the posts count as well as the user information and other post information, you will have to run a join query on both the users and posts table. Your query would now become:
$query = "SELECT u.*, p.*, count(p.id_posts) FROM users AS u JOIN posts AS p ON u.id_users = p.id_users WHERE p.id_users = 3";
Some Useful Notes
p.* - * is a wildcard character that means get all the columns in the posts table
u.* - * is a wildcard that means get all the columns in the users table
posts as p - AS is for aliasing. So, we are giving posts table a temporary name.
Here are the different types of the JOINs in SQL:
(INNER) JOIN: Returns records that have matching values in both tables
LEFT (OUTER) JOIN: Return all records from the left table, and the matched records from the right table
RIGHT (OUTER) JOIN: Return all records from the right table, and the matched records from the left table
FULL (OUTER) JOIN: Return all records when there is a match in either left or right table
Note: It is necessary that you have to join two/more tables only with the help of foreign key. Without the foreign key is is meaningless to join two or more tables
Reference 1: https://www.w3schools.com/sql/sql_join.asp
Reference 2: https://www.tutorialspoint.com/mysql/mysql-using-joins.htm
As per the Question what you have asked to join the tables
Query:
SELECT * FROM TABLE 1 JOIN TABLE 2 ON TABLE1.id = TABLE2.id WHERE TABLE2.ID=3
Kindly replace TABLE1 & TABLE2 with the Tables that are to be joined and the id with the foreign key what you have specified in the Table.
Hope so this might be helpful for you to write your own code in future. Happy Coding :)
You have only to use a simple join.
SELECT count(*)
FROM USER u,
post p
WHERE p.id_user = u.id_user
AND u.id_user = 3

joining tables with same categories

I'm trying to join 2 tables with same categories but it's not quite working, its not returning results.
I have 2 tables tvshows and movies and both of them have columns categories now i want to go through all of them and see if categories match and return them but I don't know what is wrong with this. Please take a look, thanks!
$query = $connection->prepare("
SELECT
a.id,
a.hash,
a.categories,
a.thumb_location,
a.name,
a.year,
b.id,
b.hash,
b.categories,
b.thumb_location,
b.name,
b.year
FROM movies AS a
LEFT JOIN series AS b
ON a.categories = b.categories
WHERE a.categories LIKE ? OR b.categories LIKE ? ORDER BY a.id DESC
");
You are using column named categories in WHERE part of your query, but there are two columns with this name in tables movies and series, so database won't know, which column to search in.
You have to specify probably WHERE a.categories LIKE ? OR b.categories LIKE ? if you want to find rows, where some category is set for movie or series.
If you want to search for movies and series pairs by categories, you should join them by that column. But if categories is something like "Comedy,Action" etc. it won't work. You should consider remodeling database and create table categories and then movies_categories with ID of movie and ID of category rows and series_categories with same structure. Than you will be able to do lot's of queries for selecting movies that has categories same as series etc.
I'm not sure I've understood very well, but give this a try:
$query = $connection->prepare("
SELECT a.hash,
a.categories,
a.thumb_location,
a.name,
a.year,
b.hash,
b.categories,
b.thumb_location,
b.name,
b.year
FROM movies AS a
LEFT JOIN series AS b
ON a.hash = b.hash
WHERE a.categories = b.categories ORDER BY id DESC
");

SQL Query count issue

I've two tables artist and members. artist has zero to many relation with members. (one artist may have 0 or many members)
So, I want to get all the fields of artist with the total number of members, that he has.
I write a query, but, it returns 0 count if there is no artist in the table (artist table is empty). I am using this query
SELECT a.artistname, count(m.id) as total
FROM artist a, members m
WHERE m.artist_id = a.id
It should simply not return anything if there is no artist.
Thank you!
I'm assuming that your field name is artistname in the artist table,
The query should go like this:
SELECT artist.artistname, COUNT(members.id) AS total_members
FROM artist
LEFT JOIN members ON members.artis_id = artist.id
GROUP BY artist.artistname
Use join, and to use sql count you need to use group by.
Perhaps the artistname is empty. Did you check the data? You could try the following modified query:
SELECT a.artistname, count(m.id) as total
FROM artist a, members m
WHERE m.artist_id = a.id
AND a.artistname is NOT NULL
AND a.artistname <> '';
This should work:
SELECT a.artistname,
count(m.id)
FROM artist a
LEFT JOIN members m
ON m.artist_id = a.id;

MySql find data in one table based on conditions of two other tables

Help me out with this query:
I have 3 tables with this structure.
items_to_groups (item_id | group_id)
item_to_regions (item_id | region_id)
items [a bunch of columns]
I need to select every row on the item table that has an item_id match on item_to_groups table WHERE group = x AND has an item_id match on item_to_regions table WHERE region = y
Currently the code I have is a horrible subquery with loops and all.
What would be a better way of doing this?
I've thought about JOIN and such, but can't really get my head around on how to do it.
SELECT bunch_of_columns
FROM items i
INNER JOIN items_to_groups ig ON i.id=ig.item_id
INNER JOIN items_to_regions ir on i.id=ir.item_d
WHERE ir.region_id=y
AND ig.group_id=x
Have a look at the JOIN documentation on MySQL. Joins are important for relational databases.
As you said you have a hard time grasping joins, have a look at A Visual Explanation of SQL Joins by Jeff Atwood. Maybe it helps.
SELECT colums
FROM items
INNER JOIN items_to_groups ON items.item_id = items_to_groups.item_id AND group_id = x
INNER JOIN items_to_regions ON items.item_id = items_to_regions.item_id AND region_id = y
SELECT * FROM items
JOIN items_to_groups ON (items.item_id = items_to_groups.item_id AND group_id = ?)
JOIN items_to_regions ON (items.item_id = items_to_regions.item_id AND region_id = ?)
GROUP BY items.item_id

Categories