Sql update with Joins not working

I have this query and I am returning the error "You have an error in your SQL syntax, check the manual corresponding to your MySQL server version for the correct syntax to use next to" FROM Sales JOIN Sales ON Inventory.saleID "= Sales. ID JOIN customers ON Sales.custo 'on line 1

Here is my request. I know I am not using aliases, so it looks ugly, but the syntax seems to be correct and the variables im passing inside are not null, I was returning them back.

 $sql = "UPDATE Sales SET salequantity='$takeCount' 
       FROM `Sales` 
       JOIN Sales ON Inventory.saleID=Sales.ID 
       JOIN Customers ON Sales.customerID=Customers.ID 
       JOIN Products ON Inventory.productID = Products.ID 
      JOIN Categories ON Products.categoryID=Categories.ID 
      WHERE Customers.ID='$customersID' AND Products.ID='$id'";

      

Here is my diagram

CREATE TABLE Categories
(
ID int NOT NULL auto_increment,
type varchar(255),
description varchar(255),
PRIMARY KEY (ID)
);

CREATE TABLE Products
(
ID int NOT NULL auto_increment,
categoryID int,
customerID int,
name varchar(255),
description varchar(255),
price float(10),
quantity int(10),
PRIMARY KEY (ID),
FOREIGN KEY (categoryID) REFERENCES Categories(ID)
);
CREATE TABLE Customers
(
ID int NOT NULL auto_increment,
productID int,
name varchar(255),
street varchar(255),
city varchar(255),
state varchar(255),
zip int(8),
username varchar(255),
password varchar(255),
confirm_password varchar(255),
phone varchar(255),
PRIMARY KEY (ID),
);

CREATE TABLE Sales
(
ID int NOT NULL auto_increment,
customerID int,
amount float(15),
salequantity int(10),
PRIMARY KEY (ID),
FOREIGN KEY (customerID) REFERENCES Customers(ID)
);

CREATE TABLE Inventory
(
productID int,
saleID int,
FOREIGN KEY (productID) REFERENCES Products(ID),
FOREIGN KEY (saleID) REFERENCES Sales(ID)
);

      

Here is the final request. I just want this simple one to work.

UPDATE Sales  INNER JOIN Sales ON Inventory.saleID=Sales.ID SET salequantity=2 where Sales.customerID=1 AND Inventory.productID=1;

      

Request request from first comment and failed.

UPDATE Sales SET salequantity=5 FROM Sales JOIN Inventory ON Inventory.saleID=Sales.ID JOIN Customers ON Sales.customerID=Customers.ID JOIN Products ON Inventory.productID = Products.ID JOIN Categories ON Products.categoryID=Categories.ID where Customers.ID=1 AND Products.ID=1;

      

Solution Aggregate function works fine

$customerProducts = mysql_query("select Products.ID, Products.name, Products.description, Products.quantity,Products.price, Categories.type, Sales.ID, Sales.customerID, Sales.amount, Sales.salequantity, Inventory.productID, Inventory.saleID, Customers.ID, Customers.productID, COUNT(Products.name) AS productsCount from(((( Inventory INNER JOIN Sales ON Inventory.saleID=Sales.ID) JOIN Customers ON Sales.customerID=Customers.ID) JOIN Products ON Inventory.productID = Products.ID) JOIN Categories ON Products.categoryID=Categories.ID) where Customers.ID ='$customersID' GROUP BY Products.name");

      

+3


source to share


2 answers


Scheme

create table sales (id varchar(10), customerid varchar(10), salequantity varchar(10))
create table Inventory (id varchar(10), productID varchar(10), saleID varchar(10))
create table Customers (id varchar(10))
create table Products (id varchar(10), categoryID varchar(10))
create table Categories (id varchar(10))

      

Corrected query

UPDATE Sales SET salequantity='$takeCount' 
  FROM Sales JOIN Inventory ON Inventory.saleID=Sales.ID
             JOIN Customers ON Sales.customerID=Customers.ID
             JOIN Products ON Inventory.productID = Products.ID
             JOIN Categories ON Products.categoryID=Categories.ID
  where Customers.ID='$customersID' AND Products.ID='$id'

      



Output

(0 lines (lines) affected)

Change to request

In the first join, the actual query looked like "FROM Sales

JOIN Sales ON Inventory.saleID = Sales.ID", adjusted it to "FROM Sales

     JOIN Inventory ON Inventory.saleID = Sales.ID". It is not clear what exactly is your desired outcome. But the above query resolves the compilation error.

0


source


$customerProducts = mysql_query("select Products.ID, Products.name, Products.description, Products.quantity,Products.price, Categories.type, Sales.ID, Sales.customerID, Sales.amount, Sales.salequantity, Inventory.productID, Inventory.saleID, Customers.ID, Customers.productID, COUNT(Products.name) AS productsCount from(((( Inventory INNER JOIN Sales ON Inventory.saleID=Sales.ID) JOIN Customers ON Sales.customerID=Customers.ID) JOIN Products ON Inventory.productID = Products.ID) JOIN Categories ON Products.categoryID=Categories.ID) where Customers.ID ='$customersID' GROUP BY Products.name");

      



This is exactly what I was looking for. I tried to make it complex and forgot about aggregates.

0


source







All Articles