SQL query compare with 3 tables - mysql

i have a question about sql query.
here i have 3 table which are:
Table : elaun
1. elaun_kod (pk)
2. Jenis_elaun
3. peratus_elaun
table 2 : staff_elaun
1. staff_elaunID (pk)
2. staff_ID (fk)
3. elaun_kod (fk)
table 3 : staff
1. staff_ID (pk)
So here, i want to select the 'jenis_elaun'and 'peratus_elaun' from elaun table where their 'jenis_kod' equal to 'jenis_kod' in staff_elaun table. And then from staff_elaun table, i want to compare staff_ID with staff_ID in staff table.
so here is my query but its didnt display anything
$sql1 = mysql_query("
SELECT elaun.*
FROM elaun, staff_elaun, staff
WHERE
elaun.elaun_kod = staff_elaun.elaun_kod
AND staff_elaun.staff_ID = staff.staff_ID
AND staff.staff_ID = '$data[staff_ID]'
");

You are describing a series of joins between tables. Each join has a condition for which rows correspond in each table.
In SQL, that is specified with a JOIN clause.
SELECT
elaun.*
FROM elaun
INNER JOIN staff_elaun USING (elaun_kod)
INNER JOIN staff USING (staff_ID)
Then, apply the restriction using a WHERE clause.
SELECT
elaun.*
FROM elaun
INNER JOIN staff_elaun USING (elaun_kod)
INNER JOIN staff USING (staff_ID)
WHERE
staff.staff_ID = '… the ID value you want here …'
Of course, you should not be directly injecting values into the query; instead, use query parameters. See How can I prevent SQL injection in PHP?

Related

How to Join three tables properly

The main table has 4 columns:
User Activity Table
userActivityId userId therapistId activityId
1 1 1 1
Each of these columns is a table and these values are all foreign keys.
Basically im trying to run a query that will join to the users table and pull their first and last name based off the user Id.Same thing with therapist - join to the therapist table, pull first + last name.And finally Join to the Activity table and pull the activity name and path from the activity Id
The other tables look like this:
User Table
userId fName lName
Therapist Table
therapistId therapistFirstName therapistLastName
Activity Table
activityId activityTitle activityPath
So far my query looks like
SELECT
User_Activities.userId,
User_Activities.therapistId,
User_Activities.activityId,
Activities.activityTitle,
Activities.activityPath,
Users.fName,
users.lName,
Therapists.therapistFirstName,
Therapists.therapistLastName
FROM
User_Activities
INNER JOIN Users
ON User_Activities.userId = Users.userId
INNER JOIN Therapists ON
User_Activities.therapistId = Therapists.therapistId
INNER JOIN Activities ON
Activities.activityId = User_Activities.userActivityId
WHERE
User_Activities.userId = 1;
When I run this query It only returns 1 row as a result. However there are two activities in the User_Activites table assigned to userId 1.
If I change : INNER JOIN Activities ON
Activities.activityId = User_Activities.userActivityId
from an INNER JOIN to the LEFT JOIN it will display the second row, however the activityTitle and activityPath will be displayed as NULL in the second row.
userActivityId userId therapistId activityId activityId activityTitle activityPath fName lName therapistFirstName therapistLastName
1 1 1 1 1 Brain GZZ0zpUQ S C M D
11 1 1 1 NULL NULL NULL S C M D
You have pretty much answered your question. The second activity does not have a valid ActivityId.
If you want all activities for a user, then you should phrase the query as:
SELECT . . .
FROM Users u LEFT JOIN
User_Activities ua
ON ua.userId = u.userId LEFT JOIN
Therapists t
ON ua.therapistId = t.therapistId LEFT JOIN
Activities a
ON a.activityId = ua.userActivityId
WHERE u.userId = 1;
You want to start with the table where you want to keep all the rows. Then use LEFT JOIN to bring in other tables.
Two other changes of note:
Table aliases are used to simplify reading and writing the query. The SELECT needs to change to use the aliases.
The WHERE clause refers to the Users table rather than UserActivities.

Joining four tables with left or right join [duplicate]

This question already has an answer here:
LEFT JOIN does not return all the records from the left side table
(1 answer)
Closed 4 years ago.
I have four tables.
Level Table levels(id, name)
Student Table students(id, name, level_id)
Subject Table subjects(id, name)
Report Table reports(id, student_id, subject_id, test, exam)
I need a single query that will fetch results(report table) for a single level(e.g id=1 for Class One) and a particular subject(e.g id=2 for like English)
I have this query:
select name, test, exam
from `reports`
right join `students`
on `reports`.`student_id` = `students`.`id`
where `students`.`level_id` = '1'
and `reports`.`subject_id` = '2'
But it's only showing result for records that are inside the reports table. Am expecting all data from the students table where the level_id is specified but with NULL from their test and exam row if they don't have record in the results table.
The main issue is the subject_id clause(and reports.subject_id = '2') on the query. It's only working for record if present in the records table Here is the result
When I used subject_id that's not in the result table Here is the result -not returning anything
But it's only showing result for records that are inside the reports table
Not strictly true - its only showing results where reports.subject_id = '2'. If there is no matching row in the reports table, then the subject_id would be null, and null is not equal to 2.
You need to specify the predicate for the reports table in the join clause, not in the where clause, to get all the students:
select name, test, exam
from `reports`
right join `students`
on `reports`.`student_id` = `students`.`id`
and `reports`.`subject_id` = '2'
where `students`.`level_id` = '1'
you can try below statement to get all student with level=1
select name
, test
, exam
from students
join reports
on students.id = reports.student_id
and reports.subject_id = 2
where students.level_id = 1

Insert one db's different tables data to other db's single table

i'm running into a complex problem, the data in a db has three tables.
First_DB
-- default_users
id username email password
1 Timbog Timbog#mail.com vads7y3kkjdfa
2 Marta Marta#mail.com vads7y3kkjdfa
-- default_album
album_id album_name default_user_id
1 Name_Tim 1
3 Katarina 2
-- default_album_img
img_id image_file album_id
3 1320229733.jpg 1
4 3320229733.jpg 3
Second_DB
--users
user_id user_name user_email user_pass user_image
1 Timbog Timbog#mail.com vads7y3kkjdfa 1320229733.jpg
2 Marta Marta#mail.com vads7y3kkjdfa 3320229733.jpg
The approach i used to solve this problem is to first fetch all data by inner join, should i use full outer join and insert the required field to my table, the following query is actual by which i'm trying to make it wor:
INSERT INTO bbpin.users ( user_name, user_pin, user_email, user_password, user_img)
SELECT default_users.username, default_users.bb_pin, default_users.email, default_users.password
FROM bbmpins_pins.default_users
INNER JOIN bbmpins_pins.default_album_images
ON default_album_images.album_id = default_users.id;
i miss the thing how do i compare two table's id in this join maybe? or this query is all wrong by approach?
By two tables which are sepearte in First_DB there could be multiple record how do we trunk them to last entry only ?
Thanks
It looks like you are attempting to retrieve all rows from the default_users table. And along with each row, also return the corresponding row(s) from default_album table. And along with that, the corresponding row(s) from default_album_img table.
Given the example data, a query using inner joins would return the specified result:
SELECT u.id AS user_id
, u.username AS user_name
, u.email AS user_email
, u.password AS user_pass
, i.image_file AS user_image
FROM default_users u
JOIN default_album a
ON a.default_user_id = u.id
JOIN default_album_img i
ON i.album_id = a.album_id
That query will work for the example data.
But, if there is a row in default_user which doesn't have a matching row in default_album, then an inner join won't return that row:
-- default_users
id username email password
3 branstark bran#winterfell warg2
Or, if there are two or more rows in default_album that match a given user, then the query will return two copies of the row from default_user...
-- default_album
album_id album_name default_user_id
1 Tim2 1
Without a specification of what is to be returned in those cases, we can't recommend a query.
I don't see anything wrong with your current approach using a JOIN but could modify it a bit to be more readable and also you will have to join the relation table
INSERT INTO bbpin.users (user_id, user_name, user_pin, user_email, user_password, user_img)
SELECT du.id,
du.username,
du.bb_pin,
du.email,
du.password,
dai.image_file
FROM bbmpins_pins.default_users du
JOIN bbmpins_pins.default_album da ON du.id = da.default_user_id
INNER JOIN bbmpins_pins.default_album_images dai
ON dai.album_id = da.album_id;

SQL Genius need .. Complex MySQL query

I am trying to optimise my php by doing as much work on the MySQL server as possible. I have this sql query which is pulling data out of a leads table, but at the same time joining two tags tables to combine the result. I am looking to add a company which is linked through a relations table.
So the table that holds the relationship between the two is relations_value which simply states (I add example data)
parenttable (companies) | parentrecordid (10) | childtable (leads) | childrecordid (1)
the companies table has quite a few columns but the only two relevant are;
id (10) | companyname (my company name)
So this query currently grabs everything I need but I want to bring the companyname into the query:
SELECT leads.id,
GROUP_CONCAT(c.tag ORDER BY c.tag) AS tags,
leads.status,
leads.probability
FROM `gs_db_1002`.leads
LEFT JOIN ( SELECT *
FROM tags_module
WHERE tagid IN ( SELECT id
FROM tags
WHERE moduleid = 'leads' ) ) as b
ON leads.id = b.recordid
LEFT JOIN `gs_db_1002`.tags as c
ON b.tagid = c.id
GROUP BY leads.id,
leads.status,
leads.probability
I need to be able to go into the relations_values table and pull parenttable and parentrecordid by selecting childtable = leads and childrecordid = 1 and somehow join these so that I am able to get companyname as a column in the above query...
Is this possible?
I have created a sqlfiddle: sqlfiddle.com/#!2/023fa/2 So I am looking to add companies.companyname as column to the query.
I don't know what your primary keys and foreign keys are that link each table together.. if you could give a better understanding of what ID's are linked to eachother it would make this a lot easier... however i did something that does return the correct result... but since all of the ID's are = 1 then it could be incorrect.
SELECT
leads.id, GROUP_CONCAT(c.tag ORDER BY c.tag) AS tags,
leads.status, leads.probability, companyname
FROM leads
LEFT JOIN (
SELECT * FROM tags_module WHERE tagid IN (
SELECT id FROM tags WHERE moduleid = 'leads' )
) as b ON leads.id = b.recordid
LEFT JOIN tags as c ON b.tagid = c.id
LEFT JOIN relations_values rv on rv.id = b.recordid
LEFT JOIN companies c1 on c1.createdby = rv.parentrecordid
GROUP BY leads.id,leads.status, leads.probability

Is it a join in SQL?

I have three tables.
The first table is:
Orders
======
Ordernumber PK
CartID
Field1
....
The second table is:
OrderDetails
============
Ordernumber PK
SKU FK (with InventorySuppliers localSKU field)
Field-a
.....
The third table is:
InventorySuppliers
=================
SupplierID PK
LocalSKU FK (Orderdetails table with 'SKU')
Field-x
....
I want to do is something like this:
SELECT *
FROM ORDERS
WHERE ORDERS.CARTID = 11
AND INVENTORYSUPPLIER.SUPPLIERID = 155
My problem is that there is no direct relation between the two tables used in this query.
How can I write this query?
Yes, you need a JOIN:
SELECT o.*
FROM orders o
JOIN orderdetails od ON o.ordernumber = od.ordernumber
JOIN inventorySuppliers is ON is.localSKU = od.SKU
WHERE o.cartID = 11
AND is.supplierID = 155
Because there is no direct relationship between orders and inventorySuppliers tables records, you have to use the third table (orderdetails) to join them together.
Your schema design is fatally flawed, because two different suppliers may use the same sku for different products. Your schema provides no way to know which record in the inventory suppliers the sku refers to.
Before you can do anything else with this sql, you need to change the schema to include supplierID in the order details records.