mysql select where left join syntax - php

I have a problem. I have 2 database tables.
table 1 people:
+----------+--------------+
| id | name |
+----------+--------------+
| 1 | johanalj |
| 2 | hjgjhggjh |
+----------+--------------+
table 2 images of people:
+----------+--------------+----------------+
| id | url | people_ID |
+----------+--------------+----------------+
| 1 | 3765345.png | 1 |
| 2 | 87e58974.png | 1 |
+----------+--------------+----------------+
Now I want to select person with id 1 from table 1 and all pictures from table 2 that have people_ID 1.
I tried LEFT JOIN in combination with a WHERE but cant get it to work
$sql = "SELECT * FROM people p LEFT JOIN images i ON i.people_ID = p.id WHERE id = '1'";
But I get a no result massage. What am I doing wrong?

There is an error(ambiguous column id). Both tables have id column. You need to add the table alias with id. try with -
$sql = "SELECT * FROM people p LEFT JOIN images i ON i.people_ID = p.id WHERE p.id = '1'";

Related

How do I select distinct rows and cross check in another table?

I have 3 tables, users and tasks and completed_tasks. So basically I want to select all tasks where the user_id = 2 AND also check that the task does not exist in another table` the So here is my tables:
users table:
+----+-------+
| id | name |
+----+-------+
| 1 | John |
| 2 | Sally |
+----+-------+
tasks table:
+----+-----------+---------+
| id | task_name | user_id |
+----+-----------+---------+
| 1 | mop floor | 2 |
| 2 | dishes | 1 |
| 3 | laundry | 2 |
| 4 | cook | 2 |
+----+-----------+---------+
completed_tasks table:
+----+---------+---------+
| id | task_id | user_id |
+----+---------+---------+
| 1 | 1 | 2 |
+----+---------+---------+
Here is my current SELECT code for my MySQL database:
$db = "SELECT DISTINCT tasks.task_name, users.name FROM tasks LEFT JOIN ON users.id = tasks.user_id WHERE tasks.user_id = 2";
THe problem I'm having is: I want it to search in completed_tasks table and if the task exists, then don't select that task.
I tried to do that by adding the following but it did not work:
LEFT JOIN completed_tasks ON completed_tasks.user_id = 2
That did not work because if I had multiple completed tasks, it would just ignore it all together.
I want the end result should return the user's name and task name of task 3 and 4.
Also, performance is critical in my application. I could use PHP and loop through the arrays and do SELECT for each of them but that would not be good for performance.
You have a few ways to do this.
You can use a LEFT JOIN and then check for NULL in the optional table.
SELECT a.name, b.task_name
FROM users a
JOIN tasks b ON a.id = b.user_id
LEFT JOIN completed_tasks c ON c.task_id = b.id AND c.user_id = b.user_id
WHERE c.id IS NULL
;
You can do a NOT EXISTS sub-query
SELECT a.name, b.task_name
FROM users a
JOIN tasks b ON a.id = b.user_id
WHERE NOT EXISTS (
SELECT 1
FROM completed_tasks c
WHERE c.task_id = b.id AND c.user_id = b.user_id)
;

MySQL : LEFT JOIN for 4 tables which are connected to each other

I have 4 tables as below:
1) courses
| ID - > Primary Key | Name
-------------------------------
| 1 | Course 1
| 2 | Course 2
| 3 | Course 3
2) countries
| IDPrimary Key | Name
-------------------------------
| 1 | Country 1
| 2 | Country 2
| 3 | Course 3
3) universities
| ID - > Primary Key | Name | country_id
---------------------------------------------------
| 1 | University 1 | 1
| 2 | University 2 | 1
| 3 | University 3 | 3
4) university_courses
| ID - > Primary Key | university_id | course_id
----------------------------------------------
| 1 | 1 | 2
| 2 | 3 | 2
| 3 | 3 | 3
Now, I need to create one REST API in core PHP for android app in that I will get two parameters country_id and course_id. Both will contain multiple values like country_id = "3,4,5" and course_id = "1,6,8".
I have to response all the related universities according to the country and course.
I have tried below query but i am not getting desired output so please help me if anyone have idea for my problem.
SELECT * FROM universities LEFT JOIN countries ON countries.id = universities.country_id LEFT JOIN university_courses ON universiity_courses.university_id = universities.id LEFT JOIN courses ON courses.id = university_courses.course_id WHERE FIND_IN_SET(universities.country_id, ?) AND FIND_IN_SET(university_courses.course_id, ?)
If course_id = 1,2 and country_id = 2,3, my desired output should be:
| university_id |
| 3 |
UPDATE:
Above issue is solved but now facing new issue is getting same university multiple time in result like:
if course_id = 2,3 I am getting response as:
|university_id |
------------------
|3 |
|3 |
For that I have used GROUP BY clause but getting 500 Internal Server Error if I remove GROUP BY It will work fine What can be the issue?
My new query is as below:
SELECT * FROM universities LEFT JOIN university_courses ON university_courses.university_id = universities.id LEFT JOIN courses ON courses.id = university_courses.course_id LEFT JOIN countries ON countries.id = universities.country_id WHERE FIND_IN_SET(university_courses.course_id, ?) AND FIND_IN_SET(universities.country_id, ?) GROUP BY university_courses.university_id
use LEFT OUTER JOIN not LEFT JOIN
SELECT * FROM universities LEFT OUTER JOIN countries ON countries.id = universities.country_id LEFT OUTER JOIN university_courses ON universiity_courses.university_id = universities.id LEFT OUTER JOIN courses ON courses.id = university_courses.course_id WHERE FIND_IN_SET(universities.country_id, ?) AND FIND_IN_SET(university_courses.course_id, ?)

Not Displaying Results with Inner Join 3 Tables

I am trying to inner join 3 tables that is from OS TICKET Database.
The code I am using is $qry = "SELECT qbcd_user_email.address, qbcd_user_email.user_id FROM qbcd_user_email INNER JOIN qbcd_user ON qbcd_user.id = qbcd_user_email.user_id INNER JOIN qbcd_ticket ON qbcd_ticket.user_id WHERE (qbcd_user_email.address = '.$email.') ORDER BY qbcd_ticket.ticket_id DESC";
Code is returning:
string(287) "SELECT qbcd_user_email.address, qbcd_user_email.user_id FROM qbcd_user_email INNER JOIN qbcd_user ON qbcd_user.id = qbcd_user_email.user_id INNER JOIN qbcd_ticket ON qbcd_ticket.user_id WHERE (qbcd_user_email.address = '.patrick.kershner#gmail.com.') ORDER BY qbcd_ticket.ticket_id DESC"
but it is not displaying anything in the while clause:
while ($row = mysqli_fetch_assoc($result)){
echo $row['qbcd_ticket.number]."<br>";}
I am not sure what is going on, or why its not displaying the results.
Can someone check out my code above and verify?
Try to add the number to your selected properties
$qry = "SELECT qbcd_user_email.address, qbcd_user_email.user_id, qbcd_ticket.number FROM qbcd_user_email INNER JOIN qbcd_user ON qbcd_user.id = qbcd_user_email.user_id INNER JOIN qbcd_ticket ON qbcd_ticket.user_id WHERE (qbcd_user_email.address = '.$email.') ORDER BY qbcd_ticket.ticket_id DESC"
the first table is:
qbcd_ticket:
rows:
ticket_id | number | user_id | user_email_id | status_id | dept_id | and more...
5 | 762086| 2 | 0 | 1| 1 |
the next is qbcd_user_email
rows:
id | user_id | flags | address
2 | 2 | 0 | example#demo.com
the last is: qbcd_user
id | org_id | default_email_id | status | name | created | updated
2 | 0 | 2 | 0 | Patrick Kershner | 2017-03-03 10:44:28 | 2017-03-03 10:44:28
The information that I need to display, is all corresponding Tickets associated with the customer where it = the email address.
the only static variable that will not change is $_SESSION['user_email']; which is logged by logging into the members area.

MySQL - inner join - add column with value based on other value

I'm struggling with mysql joins :/
I've multiple tables inside database fe. tasks, users etc.
Table tasks containing tasks with various variables, but the most important - id's of users signed to task (as different roles inside the task - author, graphic, corrector):
+---------+-------------+--------------+
| task_id | task_author | task_graphic |
+---------+-------------+--------------+
| 444 | 1 | 2 |
+---------+-------------+--------------+
Table users
+---------+----------------+------------+-----------+
| user_id | user_nice_name | user_login | user_role |
+---------+----------------+------------+-----------+
| 1 | Nice Name #1 | login1 | 0 |
+---------+----------------+------------+-----------+
| 2 | Bad Name #2 | login2 | 1 |
+---------+----------------+------------+-----------+
Using PDO I'm getting the whole data I want while using INNER JOIN with data from different tables (and $_GET variable)
SELECT tasks.*, types.types_name, warehouse.warehouse_id, warehouse.warehouse_code, warehouse.warehouse_description
FROM tasks
INNER JOIN types ON types.types_id = tasks.task_id
INNER JOIN warehouse ON warehouse.warehouse_id = tasks.task_id
WHERE tasks.task_id = '".$get_id."'
ORDER BY tasks.task_id
Above query returns:
+---------+--------------+--------------+----------------+------------+-----------+------------------+------------------------+------------+-------------+-----------------+-----------+----------------+--------------------+---------------------+-----------+---------------------+------------------+---------------------+
| task_id | task_creator | task_graphic | task_purchaser | task_title | task_lang | task_description | task_description_files | task_files | task_status | task_prod_index | task_type | task_print_run | task_print_company | task_warehouse_code | task_cost | task_time_added | task_deadline | task_date_warehouse |
+---------+--------------+--------------+----------------+------------+-----------+------------------+------------------------+------------+-------------+-----------------+-----------+----------------+--------------------+---------------------+-----------+---------------------+------------------+---------------------+
| 2 | 1 | 2 | 1 | Test | PL | Lorem ipsum (?) | | | w | 2222 | 3 | 456546 | Firma XYZ | 2 | 124 | 29.09.2016 15:48:20 | 01.10.2016 12:00 | 07.10.2016 14:00 |
+---------+--------------+--------------+----------------+------------+-----------+------------------+------------------------+------------+-------------+-----------------+-----------+----------------+--------------------+---------------------+-----------+---------------------+------------------+---------------------+
And I'd like to get query with added user_nice_name after task_creator, task_author and task_graphic - obviously nice names selected from table users based on ID's provide in 3 above fields fe.
+---------+--------------+------------------------------------+--------------+--------------------------------------+
| task_id | task_creator | task_creator_nn | task_graphic | task_graphic |
+---------+--------------+------------------------------------+--------------+--------------------------------------+
| 2 | 1 | Nice Name (from task_creator ID=1) | 2 | Nice Name (from task_graphic ID = 2) |
+---------+--------------+------------------------------------+--------------+--------------------------------------+
How can I achieve that?
You need three joins:
SELECT t.*,
uc.user_nice_name as creator_name,
ug.user_nice_name as graphic_name,
up.user_nice_name as purchaser_name,
ty.types_name, w.warehouse_id, w.warehouse_code, w.warehouse_description
FROM tasks t INNER JOIN
types ty
ON ty.types_id = t.task_id INNER JOIN
warehouse w
ON w.warehouse_id = t.task_id LEFT JOIN
users uc
ON uc.user_id = t.task_creator LEFT JOIN
users ug
ON ug.user_id = t.task_graphic LEFT JOIN
users up
ON up.user_id = t.task_purchaser
WHERE t.task_id = '".$get_id."'
ORDER BY t.task_id;
Notes:
Table aliases make the query easier to write and to read. They are also required because you have three references to users in the FROM clause.
This uses LEFT JOIN for the users in case some of the reference values are missing.
You need to work on your naming. It doesn't make sense that a "warehouse" id matches a "task" id. Or that a "task" id matches a "types" id. But that is how you phrased the query in your question.
The ORDER BY effectively does nothing, because all rows have the same task_id.
Assuming that the task_graphic_name is inside a table name task_graphic_table and the relation field are task_graphic_id
SELECT tasks.*
, types.types_name
, warehouse.warehouse_id
, warehouse.warehouse_code
, warehouse.warehouse_description
, users.user_nice_name
FROM tasks
INNER JOIN types ON types.types_id = tasks.task_id
INNER JOIN warehouse ON warehouse.warehouse_id = tasks.task_id
INNER JOIN users ON users.user_nice_name = tasks.task_graphic
WHERE tasks.task_id = '".$get_id."'
ORDER BY tasks.task_id
And if you need the column appear in a specific order you should explicitally call the column name in sequence eg:
SELECT tasks.col1
, task.col2
, types.types_name
, warehouse.warehouse_id
, warehouse.warehouse_code
, task.col2
, warehouse.warehouse_description
, task_graphic_table.task_graphic_name
Add two sub query in with your query. like
SELECT tasks.*,
....
....,
(select user_nice_name from users where id = tasks.task_author) AS task_creator_name,
(select user_nice_name from users where id = tasks.task_graphic) AS task_graphic_name
FROM tasks
INNER JOIN types ON types.types_id = tasks.task_id
....
....

How can I check two conditions before inserting?

I have three tables:
// Posts
+----+----------+---------------+-----------+
| id | title | content | id_author |
+----+----------+---------------+-----------+
| 1 | title1 | content1 | 1234 |
| 2 | title2 | content2 | 5678 |
+----+----------+---------------+-----------+
// Users
+----+--------+--------+
| id | name | active |
+----+--------+--------+
| 1 | jack | 1 |
| 2 | peter | 0 |
| 3 | John | 1 |
+----+--------+--------+
// Votes
+----+---------+---------+
| id | id_post | id_user |
+----+---------+---------+
| 1 | 32 | 1234 |
| 2 | 634 | 5678 |
| 3 | 352 | 1234 |
+----+---------+---------+
Now I need to check two conditions before inserting a new vote into Votes table:
The id of author and what I have passed are the same? Posts.id_user = :author (I know I can do that by a FK, but I don't want)
The account of current user is active? Users.active = 1
Also here is my query:
INSERT INTO Votes (id_post,id_user)
SELECT ?,?
FROM Posts p
WHERE p.id_user = :author limit 1;
How can I add second condition Users.active = 1 to my query?
EDIT: Actually I'm trying to don't let people be able to vote who are inactive (active = 0). For example if SO bans men, then I cannot vote to post anymore, because I (current user) am banned. So I'm pretty sure $_SESSION['id'] should be used in the query.
INSERT INTO Votes (id_post,id_user)
SELECT p.id,u.id
FROM Posts p, Users u
WHERE p.id_user = :author
AND u.id = :user
AND u.active = 1 limit 1;
then you set parameter user equal to the current user id.
EDIT: I suppose id_user in table Votes must be the voter's id, not the author of the post (correct?), so I fixed the query eliminating the JOIN.
Use and with where
INSERT INTO Votes (id_post,id_user)
SELECT ?,?
FROM Posts p, Users u
WHERE p.id_user = :author and u.active = 1 limit 1;
INSERT INTO Votes (id_post,id_user)
SELECT p.id, u.id
FROM Posts p
INNER JOIN Users u ON 1 = 1
WHERE p.id_user = :author
AND u.id = :current_user_id
AND u.active = 1
LIMIT 1;

Categories