MYSQL Join Query not deleting - php

I am trying to get the below query to delete all rows by id however the query is running fine but its not executing - no errors or rows effected. I have tripple checked all of the coloumn names in the tables and they are correct.
MYSQL:
DELETE c FROM campaigns c
JOIN campaignsFroms f ON f.campaign_id = c.id
JOIN campaignsRaw r ON r.campaignId = c.id
JOIN campaignsSubjects s ON s.campaign_id = c.id
WHERE c.id = 1582

I wonder if you want this:
DELETE c, f, r, s
FROM campaigns c LEFT JOIN
campaignsFroms f
ON f.campaign_id = c.id LEFT JOIN
campaignsRaw r
ON r.campaignId = c.id LEFT JOIN
campaignsSubjects s
ON s.campaign_id = c.id
WHERE c.id = 1582;
This will delete records from all the tables that are connected to the campaign. The LEFT JOIN is to ensure that the rows are not removed from consideration by the joins.

Related

PDO join statement

I have the following join below and I was wanting to know is there a better way to write it as I am getting Unknown table 'id' in MULTI DELETE and I cannot seem to pin point where.
Join:
$query = $dbConnection->prepare('
DELETE c.id, r.id, s.id,f.id,ip.id,ct.id
FROM campaigns c
JOIN campaignsFroms f ON f.id = c.id
JOIN campaignsRaw r ON r.id = c.id
JOIN campaignsSubjects s ON s.id = c.id
JOIN campaignIPTracking ip ON ip.id = c.id
JOIN campaignTracking ct ON ct.id = c.id
WHERE c.id = :campaign_id');
$query->execute(array(':campaign_id' => $campaign_id));
Your DELETE statement is incorrect. You should remove the fields.
DELETE
FROM campaigns c
JOIN campaignsFroms f ON f.id = c.id
JOIN campaignsRaw r ON r.id = c.id
JOIN campaignsSubjects s ON s.id = c.id
JOIN campaignIPTracking ip ON ip.id = c.id
JOIN campaignTracking ct ON ct.id = c.id
WHERE c.id = :campaign_id'

mysql join return 0 for one column with other columns intact

I wish to join multiple tables like- Categories, menus, restaurants, reviews, etc.
to return the restaurants that provide the inserted food with their prices.
Everything works except numberOfReviews in reviews table.
If a restaurant has no reviews then output should be 0 for numOfReviews column but other column values should be retrieved i.e. price, name, etc.
With following query I get all fields as null and count(numReviews) as 0:
select r.id
,r.`Name`
,r.`Address`
,r.city
,r.`Rating`
,r.`Latitude`
,a.`AreaName`
,m.`Price`
,count(rv.id)
from `categories` c, `menus` m, `restaurants` r, areas a, reviews rv
where m.`ItemName`="tiramisu"
and c.`restaurant_id`=r.`id`
and m.`category_id`=c.id
and r.`AreaId`=a.`AreaId`
and if I can't match rv.restaurant_id=r.id in where clause(obviously).
Where am I getting wrong? How do I solve this?
edited
select r.id,
r.`Name`,
r.`Address`,
r.city,
r.`Rating`,
r.`Latitude`,
a.`AreaName`,
m.`Price`,
r.`Longitude`,
r.Veg_NonVeg,
count(rv.id)
from restaurants r LEFT JOIN `reviews` rv on rv.`restaurant_id`=r.`id`
inner join `categories` c on c.`restaurant_id` = r.id
inner join `menus` m on m.`category_id` = c.id
inner join `areas` a on a.`AreaId` = r.`AreaId`
where m.`ItemName`="tiramisu"
First of all, don't use this old school syntax for the jointures.
Here is a query that may solve your problem:
SELECT R.id
,R.Name
,R.Address
,R.city
,R.Rating
,R.Latitude
,R.Longitude
,A.AreaName
,M.Price
,R.Veg_NonVeg
,COUNT(RV.id) AS numOfReviews
FROM restaurants R
INNER JOIN categories C ON C.restaurant_id = R.id
INNER JOIN menus M ON M.category_id = C.id
INNER JOIN areas A ON A.AreaId = R.AreaId
LEFT JOIN reviews RV ON RV.restaurant_id = R.id
WHERE M.ItemName = 'tiramisu'
GROUP BY R.id, R.Name, R.Address, R.city, R.Rating, R.Latitude, R.Longitude, A.AreaName, M.Price, R.Veg_NonVeg
I used explicit INNER JOIN syntax instead of your old school syntax and I modified the jointure with table reviews in order to get the expected result. The GROUP BY clause is required to use the aggregate function COUNT, every rows will be grouped by the enumerated columns (every column except the one used by the function).
Here is another solution that simplify the GROUP BY clause and allow the modification of SELECT statement without having to worry about the fact that every columns need to be part of the GROUP BY clause:
SELECT R.id
,R.Name
,R.Address
,R.city
,R.Rating
,R.Latitude
,R.Longitude
,A.AreaName
,M.Price
,R.Veg_NonVeg
,NR.numOfReviews
FROM restaurants R
INNER JOIN (SELECT R2.id
,COUNT(RV.id) AS numOfReviews
FROM restaurants R2
LEFT OUTER JOIN reviews RV ON RV.restaurant_id = R2.id
GROUP BY R2.id) NR ON NR.id = R.id
INNER JOIN categories C ON C.restaurant_id = R.id
INNER JOIN menus M ON M.category_id = C.id
INNER JOIN areas A ON A.AreaId = R.AreaId
WHERE M.ItemName = 'tiramisu'
As you can see here I added a new jointure on a simple subquery that does the aggregation job in order to provide me the expected number of reviews for each restaurant.
Hope this will help you.

Using multiple tables inner join on one key item. Can't figure out what is wrong

For the life of me I can not figure out where I have went wrong
I am pulling the data from these multiple tables but no data appearing
$result=mysql_query("SELECT * FROM chars uc
INNER JOIN zone_settings t ON uc.pos_zone = t.zoneid
INNER JOIN char_look v ON uc.charid = v.charid
INNER JOIN char_jobs y ON uc.charid = y.charid
INNER JOIN char_stats n ON uc.charid = n.charid
INNER JOIN char_profile p ON uc.charid = p.charid
WHERE `accid`='".$user["id"]."' ORDER BY `charid`");
Thanks kwolfe using LEFT JOIN and Removing the ORDER BY it works now. Here is the code.
$result=mysql_query("SELECT * FROM chars uc
LEFT JOIN zone_settings t ON uc.pos_zone = t.zoneid
LEFT JOIN char_look v ON uc.charid = v.charid
LEFT JOIN char_jobs y ON uc.charid = y.charid
LEFT JOIN char_stats n ON uc.charid = n.charid
LEFT JOIN char_profile p ON uc.charid = p.charid
WHERE `accid`='".$user["id"]."'");
Switch to LEFT JOINS to see if your missing a relationship along the way (INNER JOIN will only show data where a relationship is made for each table, in this case ALL tables.)

LEFT JOIN only returning half the needed results

I currently have:
SELECT * FROM submissions AS s
LEFT JOIN submission_category AS sc ON s.submission_category = sc.sub_cat_id
JOIN hospitals AS h ON h.hospital_id = sc.main_hospital_id
JOIN products AS p ON p.product_id = s.product_id
JOIN product_group AS pg ON pg.id = p.product_group_id
JOIN progress_steps AS ps ON sc.approval_step = ps.step_id
WHERE s.approval_status='2'
AND sc.user_id='$mask5'
The problem is that sc has 3 rows and s has 6 rows. I need to return all six rows but currently it is only returning 3, one of each from s that corresponds to sc.
Based on the suggestion I am now trying:
SELECT * FROM submissions AS s
LEFT JOIN submission_category AS sc ON (s.submission_category = sc.sub_cat_id AND sc.user_id='$mask5' )
JOIN hospitals AS h ON h.hospital_id = sc.main_hospital_id
JOIN products AS p ON p.product_id = s.product_id
JOIN product_group AS pg ON pg.id = p.product_group_id
JOIN progress_steps AS ps ON sc.approval_step = ps.step_id
WHERE s.approval_status='2'
But still getting three rows, not six?
Having columns of LEFT JOINed table[s] in WHERE "turns" LEFT JOIN into INNER (sc.user_id = '$mask5' always returns NULL or false in case there is no corresponding row in submission_category ). Move sc.user_id=... into join condition :
....
LEFT JOIN submission_category AS sc ON (s.submission_category = sc.sub_cat_id AND
sc.user_id='$mask5' )
...

How to INNER JOIN more than two categories?

I'm a little confused in here and need some help...
the situation is I've made three tables(fr_Leagues, fr_nations and fr_confeds), all i want to do is add a league which shows the name of the categories not the i.d with pagination. Here is the code:
NOW FIXED!
"SELECT
a.id as confed_id,
a.fr_short_name as confed_name,
b.id as nation_id,
b.fr_name as nation_name,
c.id as league_id,
c.fr_name as league_name"
." FROM fr_confeds as a
INNER JOIN fr_nations as b ON a.id = b.confed_id
INNER JOIN fr_leagues as c ON b.id = c.nation_id"
." LIMIT $paginate->start, $paginate->limit"
You are missing on how to link the different tables together. On each INNER JOIN, you need to have it:
INNER JOIN fr_nations ON a.<someColumn> = b.<anotherColumn> INNER JOIN fr_leagues ON a.<someColumn> = b.<anotherColumn>
USE THIS QUERY
SELECT * FROM fr_confeds as A
INNER JOIN fr_nations as B ON A.id = B.confed_id
INNER JOIN fr_leagues as C ON B.confed_id = C.league_id
LIMIT $paginate->start, $paginate->limit

Categories