SQL statement - compare two tables columns - mysql

I'm new to PHP and SQL.
I want to compare two table-columns.
This is what we have
Table 1(Review)
Table 2(Customer)
We want the Review_Customer_Id to be = to the Customer_Id in the Customer Table
How would you write this?
In my head it would be something like this:
SELECT * FROM Customer
WHERE Customer_Id = Review_Customer_Id IN (SELECT Review_Customer_Id FROM Review);

Check this Answer
SELECT (fistnameField + "" + LastnameField) AS Name FROM Customer WHERE Customer_Id IN (SELECT Customer_Review_Id FROM Review);

Related

Combine SQL query for userID with separate query for 2nd table

I'm trying to create an SQL query which filters information by customerID in one table and then combine that with information in another table. I have this query which filters by customerID
$query = mysqli_query($connection, "
SELECT *
FROM booked_activities
WHERE customerID LIKE $_SESSION[customerID]
");
/* INNER JOIN activities ON booked_activities.activityID = activities.activityID);*/
The second part I commented out is where I tried to combine the first half of the query with another table.
The activityID is a common ID in tables: booked_activites and activities.
Right now I can output the first query as activityID but I need to use the activityID to output the activityName from the activities table.
have you tried
$query = mysqli_query($connection, "SELECT *,ac.activityName FROM booked_activities INNER JOIN activities ac ON booked_activities.activityID = activities.activityID
WHERE customerID LIKE $_SESSION[customerID] );
also I would suggest using prepared statement for passing custometr id, instead of using sesstion variable directly in string.
SELECT * FROM booked_activities t1
join activities t2 on t2.activityID = t1.activityID
WHERE t1.customerID LIKE $_SESSION[customerID]

using subselect in mysqli

I have a query which works currently, but we added more the possibility of one customer doing more than one test drive. this additional test Drive is on a different table. The customer ID is an auto increment number on the testdriveform table. So I need to check for addition entries, based on the customer id from table 1 in table 2 and list them along withe the rest.
SELECT * FROM testdriveform Where SalesAgent not like '%Test%' and SalesAgent like '%$Sales%' and Date between '$sDate' and '$eDate' and Stock != '' order by Date, SalesAgent;
I need to add a subselect I think. Something that adds
SELECT * FROM testdrives WHERE custID = testdriveform.id
phpmyadmin is down on bluehost..again. Anyone?
NO, you don't need a subselect; rather need a JOIN between the tables like
SELECT td.*, tdf.*
FROM testdriveform tdf
JOIN testdrives td ON td.custID = tdf.id
Where tdf.SalesAgent not like '%Test%'
and tdf.SalesAgent like '%$Sales%'
and tdf.`Date` between '$sDate' and '$eDate'
and tdf.Stock != ''
order by tdf.`Date`, tdf.SalesAgent;

MySQL INSERT INTO table 1 SELECT table 2 with different column name

I have a table (pdt_1) in database (db_1) and another table (pdt_2) in another database (db_2).
I met pdt_1 and pdt_2 to find pdt_1 products not present and published in pdt_2.
functional code :
SELECT * FROM db_1.pdt_1 AS lm
WHERE lm.product_sku
NOT IN (SELECT DISTINCT product_cip7 FROM db_2.pdt_2)
AND lm.product_publish=‘Y'
finally, I need to insert the result of this query in pdt_2.
However, the structure of pdt_1 and pdt_2 are different.
Example:
- columns's names
- columns's numbers
I also need an auto_increment id for pdt_1 products inserted into pdt_2.
I need help.
NB : sorry for my poor english :(
If you want a new table with just the id and product_sku, try:
INSERT INTO new_table # with id and product_sku from first table
SELECT pdt_1.id,
pdt_1.product_sku
FROM db_1.pdt_1
LEFT JOIN db_2.pdt_2
ON pdt_1.product_sku = pdt_2.product_cip7
WHERE pdt_2.product_cip7 IS NULL
AND pdt_1.product_publish = 'Y'

update field from another select query

i am stuck with a simple query if someone can help me on on the same.
i want to update one field with query from another table
the table structure is as follows:-
table stockmain - fields - itemcode, avgcost
table sales - fields - itemid, saleprice, costprice(this field is to be generated with query from stockmain table(avgcost field)
the query is follows:-
$qry = "UPDATE sales SET costprice = SELECT avgcost FROM stockmain WHERE itemcode = 'sales.itemid' ";
You can join these two tables to get one data set, and then copy data from one field to another, e.g. -
UPDATE sales sl
JOIN stockmain stm
ON stm.itemcode = sl.itemid
SET sl.costprice = stm.avgcost;

Getting multiple values from a query

I write a query to get values from 3 tables but this is returning multiple values so can any one tell where i went wrong
select c.CompanyName,cd.FedTaxID,cd.EmailAddress,cd.PhoneNumber
from tblcustomerdetail cd,tblcustomer c
where c.FedTaxID in (
select FedTaxID
from tblcustomer
where CustomerID in (
select LOginID
from tbluserlogindetail
where UserName like "pa%" and RoleTypeID='20'
)
)
and cd.FedTaxID in (
select FedTaxID
from tblcustomer
where CustomerID in (
select LOginID
from tbluserlogindetail
where UserName like "pa%" and RoleTypeID='20'
)
);
My relation is here
My 3 tables are `tbluserlogindetails, tblcustomerdetails and tblCustomer'
1) Initially i will get `Login ID` from `tblUserLoginDetail ` based on the `user name`.
2) Next based on `LoginID` i will get `FedTaxID` from tblcustomerDetail`
3) Next based on 'FedTaxID' i will get the the required details from `tblcustomer'
SELECT
tblcustomer.CompanyName,
tblcustomerdetail.FedTaxID,
tblcustomerdetail.EmailAddress,
tblcustomerdetail.PhoneNumber
FROM tbluserlogindetail, tblcustomer, tblcustomerdetail
WHERE
tbluserlogindetail.LOginID = tblcustomer.CustomerID
AND tblcustomer.FedTaxID = tblcustomerdetail.FedTaxID
AND tbluserlogindetail.UserName LIKE 'pa%'
AND tbluserlogindetail.RoleTypeID = '20'
Try something like this.
Subqueries have a slow perfomance.
MySQL - SELECT WHERE field IN (subquery) - Extremely slow why?