SQL Join Issue PHP - php

I'm trying to do a SQL join to return records from one table for users who don't have a corresponding entry in a second table against the record in the first table. The query I'm trying is below, which I think should return what I want, but it's giving a syntax error:
$query = "SELECT * FROM `suppliers` LEFT OUTER JOIN `matches` ON suppliers.supplierid = matches.supplier_id WHERE '" . $_SESSION['userEmail'] . "' NOT IN matches.user";
Any ideas or suggestions? What I'm trying to achieve from the above is to only show results for which the logged in user hasn't marked the supplier record as viewed.
TIA

$query = "SELECT * FROM `suppliers` LEFT OUTER JOIN `matches` ON suppliers.supplierid = matches.supplier_id WHERE matches.user NOT IN '" . $_SESSION['userEmail'] . "'";
not in clause not added properly.

correct your where clause :
$query = "SELECT * FROM `suppliers` LEFT OUTER JOIN `matches` ON suppliers.supplierid = matches.supplier_id WHERE matches.user NOT IN ('" . $_SESSION['userEmail'] . "') ";
OR if it is single value then you can use != instead of not in
$query = "SELECT * FROM `suppliers` LEFT OUTER JOIN `matches` ON suppliers.supplierid = matches.supplier_id WHERE matches.user != '" . $_SESSION['userEmail'] . "' ";

You should swap your WHERE Conditions and trim with brackets ().
$query = "
SELECT * FROM `suppliers` LEFT OUTER JOIN `matches`
ON suppliers.supplierid = matches.supplier_id
WHERE matches.user NOT IN ('" . $_SESSION['userEmail'] . "')
";

Your logic is just not correct. You can use LEFT JOIN, but the query should look like:
SELECT s.*
FROM suppliers s LEFT OUTER JOIN
matches m
ON m.supplierid = s.supplier_id AND
m.user IN ('" . $_SESSION['userEmail'] . "')
WHERE m.supplierid IS NULL;
Note: This row may be causing a syntax error or unexpected results:
m.user IN ('" . $_SESSION['userEmail'] . "')
You will need to look at the syntax after variable substitution to figure that one out. This answer addresses the logic of the query, so it will do what you actually want when the syntax is fixed.

Related

combine left join into my queries but failed

Tried to combine another query to my existing queries but failed.
Here is the working query without LEFT JOIN
$order_query = $this->db->query("SELECT * FROM " . DB_PREFIX .
"order_items WHERE order_id = '" . (int)$order_id . "'");
Below is the one tried to combined (not working)
$order_query = $this->db->query("SELECT * FROM " . DB_PREFIX .
"order_items WHERE order_id = '" . (int)$order_id . "'" LEFT JOIN .
DB_PREFIX ."item_description WHERE item_id= '" . (int)$item_id . "'"
);
Any idea where did i do wrong ?
You need to construct the join statement correctly. The WHERE clauses come in later.
$prefix = DB_PREFIX;
$sql = "
SELECT oi.* FROM {$prefix}order_items AS oi
LEFT JOIN {$prefix}item_description AS itd
ON oi.item_id = itd.item_id
WHERE oi.order_id = ?
AND itd.item_id = ?
";
$this->db->query($sql, [$order_id, $item_id]);
You can try this code
$order_query = $this->db->query("SELECT oi.* FROM " . DB_PREFIX ."order_items oi
LEFT JOIN ".DB_PREFIX ."item_description des on oi.order_id = des.order_id
WHERE oi.order_id = '" . (int)$order_id . "' and
des.item_id= '" . (int)$item_id . "'");

PHP/MYSQL: Using 'CONCAT' and 'AND' clause in MYSQL Query

I'm trying to execute this query:
Query 1:
$query = "SELECT a.*, b.title_wo
FROM `worksheet_master` AS a
INNER JOIN `work_order` AS b ON a.wo_number = b.wo_number
WHERE CONCAT (a.`wo_number` like '" . $_POST["keyword"] . "%',
`title_wo` like '" . $_POST["keyword"] . "%')
ORDER BY a.`wo_number` DESC LIMIT 0,50";
Query 2:
$query = "SELECT a.*, b.title_wo
FROM `worksheet_master` AS a
INNER JOIN `work_order` AS b ON a.wo_number = b.wo_number
WHERE CONCAT (a.`wo_number` like '" . $_POST["keyword"] . "%',
`title_wo` like '" . $_POST["keyword"] . "%')
AND a.`status` = 'NULL'
ORDER BY a.`wo_number` DESC
LIMIT 0,50";
The Query 2 didn't gave me any result with AND clause while the Query 1 gave me the result.
Can anyone help me with this? I need to sort out the result which has the empty status in my table, that's why I added AND clause in Query 2 hoping the result will be as expected, but it's not.
Thank You.
Unless NULL is an actual string, you need to use IS NULL instead.
$query = "SELECT a.*, b.title_wo
FROM `worksheet_master` AS a
INNER JOIN `work_order` AS b ON a.wo_number = b.wo_number
WHERE CONCAT (a.`wo_number` like '" . $_POST["keyword"] . "%',
`title_wo` like '" . $_POST["keyword"] . "%')
AND a.`status` IS NULL
ORDER BY a.`wo_number` DESC
LIMIT 0,50";

inner join query don't return any things

I have a php code that use a mySQL query in it.
tables users and conversation and main_profile have true values to select after this query but query return nothing.
$sqlQuer =
"SELECT `conversation`.from,`conversation`.text, `main_profile`.nik_name, `users`.id
FROM `conversation`
INNER JOIN `users` ON(`users`.username = `conversation`.to )
INNER JOIN `main_profile` ON(`users`.id = `main_profile`.user_id)
WHERE `conversation`.to = '".$to."'
AND `conversation`.read = '0'";
$result = mysql_query( $sqlQuer) or die (mysql_error());
while($row = mysql_fetch_array($result))
{
echo $row[0] . "#TEXT#" . $row[1]. "#TEXT#" . $row[2] . "#CON#";
$ids[] = $row['id'];
}
any Idea ?
Query was solved with this change that First INNER JOIN changed to LEFT JOIN and second changed to RIGHT JOIN.

mysql multiple tables query command to php array - getting error

Below is my SQL Query which i am able to run successfully in PhpMyAdmin .
[ data from multiple tables similar to IMDB database ]
SELECT
`data`.`code`,
`data`.`movie`,
`movies`.`id`,
`movies`.`name`,
`data`.`person`,
`persons`.`name`,
`persons`.`code`,
`data`.`mode`,
`modes`.`name`
FROM
`data`
INNER JOIN `movies` ON (`data`.`movie` = `movies`.`id`)
INNER JOIN `persons` ON (`data`.`code` = `persons`.`code`)
INNER JOIN `modes` ON (`data`.`mode` = `modes`.`code`)
where `data`.`movie`=1
Below is PHP CODE generated by PhpMyAdmin (create PHP Code button) from above command but not fetching any results when used in PHP page (webpage) ,
$sql = "SELECT \n"
. " `data`.`code`,\n"
. " `data`.`movie`,\n"
. " `movies`.`id`,\n"
. " `movies`.`name`,\n"
. " `data`.`person`,\n"
. " `persons`.`name`,\n"
. " `persons`.`code`,\n"
. " `data`.`mode`,\n"
. " `modes`.`name`\n"
. "FROM\n"
. " `data`\n"
. " INNER JOIN `movies` ON (`data`.`movie` = `movies`.`id`)\n"
. " INNER JOIN `persons` ON (`data`.`code` = `persons`.`code`)\n"
. " INNER JOIN `modes` ON (`data`.`mode` = `modes`.`code`)\n"
. "where `data`.`movie`=1 LIMIT 0, 30 ";
Try
$sql = "SELECT
`data`.`code`,
`data`.`movie`,
`movies`.`id`,
`movies`.`name`,
`data`.`person`,
`persons`.`name`,
`persons`.`code`,
`data`.`mode`,
`modes`.`name`
FROM
`data`
INNER JOIN `movies` ON (`data`.`movie` = `movies`.`id`)
INNER JOIN `persons` ON (`data`.`code` = `persons`.`code`)
INNER JOIN `modes` ON (`data`.`mode` = `modes`.`code`)
where `data`.`movie`=1";
The \n and . from your question are not required.

mysql query selecting wrong field

$something_else = mysql_query('SELECT image_id FROM items p LEFT JOIN list up ON p.item_id = up.item_id WHERE up.UserID = "' . $user_id . '"');
while ($r=mysql_fetch_assoc($something_else)){
foreach($r as $item_id2)
$query ='DELETE FROM list WHERE UserID="' .$user_id. '" AND item_id="' .$item_id2. '"';}
This is for a product "wish list." Each is relative to a user. I can add to the wish list, but I can't delete the proper item. What this code is doing is deleting the last item on the list, or perhaps the item with the highest 'image_id'.
Either way, I'm not getting the relative 'image id' pertaining to the the associated item.
I'm pretty sure this should be enough information to solve the problem. I'm most certain my problem lies in the MySQL query:
mysql_query('SELECT image_id FROM items p LEFT JOIN list up ON p.item_id = up.item_id WHERE up.UserID = "' . $user_id . '"');
first of all as mentioned before your queries are NOT secure, please use PDO. anyways try this :
$something_else = mysql_query('SELECT image_id FROM items p LEFT JOIN list up ON p.item_id = up.item_id WHERE up.UserID = "' . $user_id . '"');
while ($r=mysql_fetch_assoc($something_else)) {
$query ='DELETE FROM list WHERE UserID="' .$user_id. '" AND item_id="' .$r['image_id'] . '"';
}

Categories