How to select from two tables and display only what you want? - php

I want to select from 2 different tables.
In the first table I want to select all, but I will display only what I want.
In the second table I want to select only the profile picture but even if a user does not have a profile picture his data from the user table should be selected.
I am using inner joins. Below is my code:
SELECT * FROM
tish_user INNER JOIN tish_images
ON tish_user.user_id = tish_images.user_id
WHERE tish_images.prof_image = 1

To select from two different tables, you should specify values from each table that you want, not using catch-all *. Using a LEFT JOIN instead of an INNER JOIN lets you connect the tables you are querying from on a single point. You can query any kind of relationship between the tables at that point.
This query will give you all the userids in tish_user returning the matching tish_images.prof_image record if prof_image is 1, NULL otherwise.
SELECT
tish_user.user_id,
tish_images.prof_image
FROM
tish_user
LEFT JOIN tish_images
ON tish_user.user_id = tish_images.user_id
AND tish_images.prof_image = 1

Try this way
SELECT * FROM
tish_user, tish_images
WHERE tish_user.user_id = tish_images.user_id
AND
tish_images.prof_image = 1;
I think this might help you.
Cheers bro

Use LEFT JOIN instead of INNER JOIN.
SELECT * FROM
tish_user LEFT JOIN tish_images
ON tish_user.user_id = tish_images.user_id
WHERE tish_images.prof_image = 1
Explanation
LEFT JOIN selects all rows in the left table, even if there are no entries in the right table (in which case the columns for the right table will be NULL)
Also check RIGHT JOIN, it does the same thing with the right side :)

Try this:
SELECT *
FROM
tish_user U
LEFT JOIN tish_images I
ON U.user_id = I.user_id
AND = I.prof_image = 1

Try this:
Suppose you want to display the userID, firstname and lastname from the tish_user table and the prof_image from the tish_images table.
SELECT tish_user.userd_id, tish_user.firstname, tish_user.lastname, tish_images.prof_image
FROM tish_user tish_user LEFT JOIN tish_image tish_image ON tish_user.user_id=tish_images.user_id WHERE tish_image.prof_image=1
I think this will do.

Related

MySQL: Accounting for NULL or 0 values when multiple joins exist on one table

I have a query that looks up people's full names based on their record ID's in a table called users. The full names are tied to their roles in another table (table1). This requires multiple joins to the users table:
SELECT table1.id, users.full_name AS "Requester",
users.full_name AS "Approver,"
users.full_name AS "Ordered By",
users.full_name AS "Received By"
FROM table1
JOIN users AS users
ON table1.requester_id = users.id
JOIN users AS users2
ON table1.approver_id = users2.id
JOIN users AS users3
ON table1.ordered_by = users3.id
JOIN users AS users4
ON table1.received_by = users4.id
WHERE table1.deleted_record !=1;
The problem I'm having is with ordered_by and received_by. Often, they don't yet exist, because the order has neither been ordered nor received, so the ID for each can be 0, which has no corresponding value in the userstable. When I run this query, I should get back all 475 records that exist, but I only get back 365, because of those 0 values. How can I modify this query to make sure all rows are returned, even if ordered_by and/or received_by = 0?
First, your primary table driving the query should be table1. Then, you are using JOIN instead of LEFT JOIN. LEFT JOIN will give you a null result if no link, but not fail. In which case, you might have to use an IF for your fields value
SELECT table1.id, req.full_name AS "Requester",
app.full_name AS "Approver",
ordr.full_name AS "Ordered By",
rec.full_name AS "Received By"
FROM table1
LEFT JOIN users AS req
ON table1.requester_id = req.id
LEFT JOIN users AS app
ON table1.approver_id = app.id
LEFT JOIN users AS ordr
ON table1.ordered_by = ordr.id
LEFT JOIN users AS rec
ON table1.received_by = rec.id
WHERE table1.deleted_record !=1;
This should do it
You are looking for left join:
SELECT t1.id, ur.full_name AS "Requester",
ua.full_name AS "Approver,"
uo.full_name AS "Ordered By",
urv.uo AS "Received By"
FROM table1 t1 LEFT JOIN
users ur
ON t1.requester_id = ur.id LEFT JOIN
users ua
ON t1.approver_id = ua.id LEFT JOIN
users uo
ON t1.ordered_by = uo.id LEFT JOIN
users urv
ON t1.received_by = urv.id
WHERE t1.deleted_record <> 1;
Note that I changed the aliases on the users references from fairly meaningless u1, u2, etc. to ua, uo, and so on. Also, these need to be used in the SELECT to get the right full name.

MySQL INNER JOIN query issue

I have created an INNER JOIN query as shown below and was wondering how I can make it work? I need for the HomeTeam and AwayTeam to equal TeamID in the query. Any help would be much appreciated. Thanks
$result = mysqli_query($con,"SELECT results.*,team.TeamName
FROM results
INNER JOIN team ON team.TeamID = results.HomeTeam
INNER JOIN team on team.TeamID = results.AwayTeam");
You need to use aliases for the table that you are including twice. Otherwise mysql cannot distinguish between the two.
To be able to process the results easily, you can do the same with the names you are selecting.
Something like:
SELECT
results.*,
t1.TeamName AS TeamNameHome,
t2.TeamName AS TeamNameAway
FROM results
INNER JOIN team t1
ON t1.TeamID = results.HomeTeam
INNER JOIN team t2
ON t2.TeamID = results.AwayTeam

PHP How can I fetch data from normalized tables

I have normalized tables I want to select the items that belong to the userid
I'm familiar with select syntax but I'm very weak in joins tables so I'm a bit confused on how to get the items that belong to the user should I use join ? or is there other way
this is just simple example of my tables they have more fields
..........
user
..........
userid
firstname
address
..........
items
..........
itemsid
itemName
itemDescription
..........
user_items
..........
userid(FK)
itemsid(FK)
Use two inner join
select a.*, b.*
from user_items as c
inner join user as a on a.userid = c.userid
inner join items as b on b.itemsid = c.itemsid;
Use INNER JOIN
SQL
select user.*, items.*
from user_items
inner join user on user.userid = items.userid
inner join items on items.itemsid = user_items.itemsid;
So if I read this correctly, user_items.userid = user.userid.
So you want to join, something like this.
SELECT i.itemsid, i.itemName, i.Description FROM items i JOIN users us ON ui.userid = us.userid JOIN user_items ui ON ui.itemsid = i.itemsid WHERE ui.userid = VALUE;
Replace Value with your actuall user id

MySQL join multiple tables error

I struggle to use join on multiple tables. When I try to do this:
SELECT `absences`.*, `employee`.*, `type`.*
FROM `absences`, `type`
LEFT JOIN `login`.`employee` ON `absences`.`employee_FK` = `employee`.`employee_ID`
I get this:
Unknown column 'absences.employee_FK' in 'on clause'
'absences.employee_FK' exists in my DB.
I want to display the user data and the type of the absence. How can I do that? I dont understand joins too well yet.
Looks like your just trying to join two tables, because you don't have a join condition for the type table in your query:
SELECT *
FROM absences
LEFT JOIN employee ON absences.employee_FK = employee.employee_ID
If you want to join to the type table too:
SELECT *
FROM absences
LEFT JOIN type ON absences.type_FK = type.type_ID
LEFT JOIN employee ON absences.employee_FK = employee.employee_ID
You have to select all the tables for using the JOIN condition.
The example goes like this:
SELECT `employee.*`, `absences.*`, `type.*`
FROM `employee`
JOIN `absences`
ON `employee`.`employee_ID` = `absences`.`employee_FK`
JOIN `type`
ON `absences`.`type_FK` = `type`.`type_ID`
JOIN `on_off`
ON `on_off`.`on_off_ID` = `employee`.`on_off_FK`;
You can modify the query as per your requirement.
You can work on the script below. Add Where clause at the end if necessary. Not tested...
SELECT * from absences a
inner join type t on (t.typeID = a.type_FK)
inner join employee e on (e.employee_ID = a.employee_FK)
This might be what you are looking for
select * from `absences` a
left outer join `employee` e on e.`employee_ID` on a.`employee_FK`
left outer join `type` t on t.`type_ID`=a.`type_FK`
left outer join `on_off` o on o.`on_off_ID`=e.`on_off_FK`
You have to use join for all tables:
SELECT `absences`.*, `employee`.*, `type`.*
FROM `absences`
JOIN `type` on `absences`.`type_fk` = `type`.`type_ID`
LEFT JOIN `login`.`employee` ON `absences`.`employee_FK` = `employee`.`employee_ID`

SQL /php LEFT JOIN - Only display one instance

I am using the following query which works great but I need a modification to it.
SELECT *
FROM `table1` LEFT JOIN
`table2` ON table1.messageid=table2.messageid
WHERE `venue_active` = 1
My problem here is that I table2.messageid might have more than 1 match and I only what 1 displayed, but I still want all the records on table1 to be returned.
For example:
table1.messageid might have lots of children on Table2 and I only need 1 displayed
How can I do this?
UPDATE: I've tried adding: GROUP BY table1.messageid but it returns nothing.
add a group by clause
SELECT *
FROM `table1` LEFT JOIN
`table2` ON table1.messageid=table2.messageid
WHERE `venue_active` = 1
GROUP BY table1.messageid
just add group by
SELECT *
FROM `table1` LEFT JOIN
`table2` ON table1.messageid=table2.messageid
WHERE `venue_active` = 1
GROUP BY table1.messageid
I don't have your exact tables but this is the equivilent of your query running off a couple of my tables which are 1:many relationships tblusers being your table1 and tblmessages being your table2
SELECT tblusers.ID FROM `tblusers` LEFT JOIN tblmessages ON tblusers.ID=tblmessages.recipient_user_id WHERE tblmessages.state=1
This returns this
If I now change the query to be this
SELECT DISTINCT tblusers.ID FROM `tblusers` LEFT JOIN tblmessages ON tblusers.ID=tblmessages.recipient_user_id WHERE tblmessages.state=1
I now get a unique list of all users which have at least one message in my messages table.

Categories