I want to delete an entry in my PAGES table. Upon deletion of it, it will cascade to OBJECTS table. No worries in here, it's working if I delete the entry by using simple DELETE. However, I need to specify some conditions:
PAGES table
+--------------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------------+--------------+------+-----+---------+----------------+
| page_id | int(11) | NO | PRI | NULL | auto_increment |
| users_id | int(11) | NO | MUL | NULL | |
| page_value | varchar(20) | NO | UNI | NULL | |
+--------------------------+--------------+------+-----+---------+----------------+
OBJECTS table
+----------------------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------------+-------------+------+-----+---------+----------------+
| objects_id | int(11) | NO | PRI | NULL | auto_increment |
| page_id | int(11) | NO | MUL | NULL | |
| objects_name | varchar(50) | NO | | NULL | |
| objects_avail | varchar(20) | NO | | NULL | |
+----------------------------+-------------+------+-----+---------+----------------+
If objects_avail == "ALL", I must not include that entry in cascade delete. I came up with this SQL query but got an error:
$query = "
DELETE FROM pages AS p
INNER JOIN objects AS o ON p.page_id = o.page_id
WHERE p.page_id = ?
AND p.users_id = ?
AND p.page_value = ?
AND o.objects_avail != ?";
The error thrown:
["42000",1064,"You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to
use near 'AS p INNER JOIN objects AS o ON p.page_id = o.page_id WHER'
at line 1"]
Example value for the PDO placeholders:
$params = array(81,5,"main page","ALL");
where all of this is valid and I'm sure this is not where the problem is.
I doubt or prettry sure I'm missing some in my query, any suggestions please?
For an inner join UPDATE or DELETE, you need to specify which of the tables you actually want to delete explicitly, or else the parser won't know what you mean. You can choose 1 or more tables to delete from. In your case, it makes sense to just delete p, the alias for pages.
DELETE p
FROM pages AS p
INNER JOIN objects AS o ON p.page_id = o.page_id
WHERE
p.page_id = ? AND
p.users_id = ? AND
p.page_value = ? AND
o.objects_avail != ?
The only line I changed was DELETE became DELETE p
Related
i have two tables
Cursos
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| curso | varchar(255) | NO | | NULL | |
+-------+------------------+------+-----+---------+----------------+
Trienios
+--------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| data_trienio | varchar(255) | NO | | NULL | |
| curso_id | int(11) | NO | | NULL | |
| oe_id | int(11) | NO | | NULL | |
+--------------+------------------+------+-----+---------+----------------+
those tables are connected through a relationship (as seen through curso_id), and i want to make a query where i retrieve the curso records and the number of trienio records related to each one of them
so i've done this query in laravel
$curso = Curso::select([
'cursos.curso',
\DB::raw('count(trienios.curso_id) as count')
])->join('trienios', 'trienios.curso_id', '=', 'cursos.id')
->groupBy('trienios.curso_id');
which translates to this
select `cursos`.`curso`,
count(trienios.curso_id) as count
from `cursos`
inner join `trienios`
on `trienios`.`curso_id` = `cursos`.`id`
group by `trienios`.`curso_id`
and it gets me the number of cursos with trienios related to them. HOWEVER, it only gives those who have a number of trienios related to them. the other ones who dont have trienios related to them are not queried, and i want to query them. so, how do i solve this issue ?
Use leftJoin(), select cursos.id, and group by cursos.id:
$curso = Curso::select([
'cursos.id',
DB::raw('count(trienios.curso_id) as count')
])->leftJoin('trienios', 'trienios.curso_id', '=', 'cursos.id')
->groupBy('cursos.id');
This is the query I want to run:
SELECT c.id,
COUNT(t.curso_id) AS count
FROM cursos c
LEFT JOIN trienios t
ON t.curso_id = c.id
GROUP BY c.id
The LEFT JOIN should prevent records from cursos from being dropped even if they have no matching counterparts in trienios. As for the error you were seeing, you were selecting cursos.curso, which is not an aggregate and which does not appear in your GROUP BY clause. When MySQL is in only_full_group_by this is not allowed, hence I changed the select clause to cursos.id.
I have the following tables:
Course
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| c_id | bigint(20) | NO | PRI | NULL | auto_increment |
| c_name | varchar(255) | NO | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
Articles
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| a_id | bigint(20) | NO | PRI | NULL | auto_increment |
| a_name | varchar(255) | NO | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
Course_Articles
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| ca_id | bigint(20) | NO | PRI | NULL | auto_increment |
| a_id | bigint(20) | NO | | NULL | |
| c_id | bigint(20) | NO | | NULL | |
| t_id | bigint(20) | NO | | NULL | |
| sort_order | int(11) | NO | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
Term (or semester)
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| t_id | bigint(20) | NO | PRI | NULL | auto_increment |
| t_name | varchar(255) | NO | | NULL | |
| c_id | bigint(20) | NO | | NULL | |
| sort_order | int(11) | NO | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
I need to present the data from these tables as follows:
Course Name: Modern Gas Extraction
Articles:
Intro
Overview of gas extraction techniques
Term 1: Conventional Techniques
Coal Mine Methane
Another conventional technique
Another article not in a term
Another one
Term 2: Unconventional Techniques
Underground Coal Gasification
Coal Bed Methane
Hydraulic Fracturing
Conclusions
I know this data would benefit from perhaps nesting/tree. But it is what I have to work with. As you can see Articles can belong to a Term or be free standing.
I am a bit stumped as to how to efficiently query the above to output as per the example.
<?php
// UPDATED with UNION as suggested
try {
$stmt = $dbh->prepare(
"SELECT ca . * , a.a_name, t.t_name
FROM Course_Article AS ca
LEFT JOIN Article AS a ON a.a_id = ca.ca_id
LEFT OUTER JOIN Term AS t ON t.t_id = ca.t_id
WHERE ca.c_id = '2'
UNION SELECT te.t_name, te.t_id, te.c_id
FROM Term AS te
WHERE te.t_id = '2'");
$last_term_id = -1;
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
if ($last_term_id != $row['t_id']) {
echo "<b>" . $row['t_name'] . "</b><br />";
$last_term_id = $row['t_id'];
}
echo $row['a_name'] . "<br />";
}
} catch(PDOException $e) {
echo 'Error : '. $e->getMessage();
exit();
}
?>
Thank you
So here is one idea how to do it with union: first part gets all the terms (also the empty ones) and articles under them, and second part gets all the additional articles that belong to the course but not to any term. Hopefully I didn't include too many logic/typo mistakes here.
select t.t_name, ca.*, a.a_name
from Term t
left outer join Course_Article ca ON ca.t_id = t.t_id
left outer join Article a ON a.a_id = ca.a_id
where t.c_id = '2'
UNION
select null as t_name, ca.*, a.a_name
from Course_Articles ca
left outer join Article a ON a.a_id = ca.a_id
where ca.c_id = '2' and ca.t_id is null
Additionally, if you would need to order the result a bit (dunno if the sorting order is generic between the course article and term), you can extend this a bit (add whatever columns you need to the final result into the main select):
select t_name, a_name, sort_column
FROM
(
select t.t_name, ca.*, a.a_name, t.sort_order as sort_column
from Term t
left outer join Course_Article ca ON ca.t_id = t.t_id
left outer join Article a ON a.a_id = ca.a_id
where t.c_id = '2'
UNION
select null as t_name, ca.*, a.a_name, ca.sort_order as sort_column
from Course_Articles ca
left outer join Article a ON a.a_id = ca.a_id
where ca.c_id = '2' and ca.t_id is null
) dt
order by sort_column ASC
I have these tables:
tbl_posts:
+---------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| date | varchar(10) | NO | | NULL | |
| content | text | NO | | NULL | |
| status | tinyint(1) | NO | | NULL | |
| person_id | int(10) | NO | | NULL | |
+---------------+------------------+------+-----+---------+----------------+
tbl_comments:
+---------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| comment | varchar(10) | NO | | NULL | |
| post_id | int(10) | NO | | NULL | |
| person_id | int(10) | NO | | NULL | |
+---------------+------------------+------+-----+---------+----------------+
and tbl_person which has person_id and some other fields...
i want to select all posts posted by a certain person and all comments for each of those posts (just like facebook)
i tried this query but it just bring me one comment for each post
select * from tbl_posts right join tbl_comments on tbl_posts.id = tbl_comments.post_id join tbl_person on tbl_person.id = tbl_posts.Person_id and tbl_comments.Person_id = 3 group by tbl_comments.id
If your foreign keys are set up correctly, an inner join should be fine. And, you don't need the group by:
select *
from tbl_posts join
tbl_comments
on tbl_posts.id = tbl_comments.post_id join
tbl_person
on tbl_comments.Person_id = 3;
Actually, I suspect the problem is tbl_person.id = tbl_posts.Person_id. Your query was returning only people who commented on their own posts.
When joining tables, you should always make sure there is a relationship between the tables you wish to join. In your case, this should give you all posts posted by a certain person and all comments for each of those posts:
SELECT *
FROM tbl_posts
LEFT JOIN
tbl_comments ON tbl_posts.id = tbl_comments.post_id
LEFT JOIN tbl_person ON tbl_comments.Person_id = tbl_person.id;
So instead of doing
tbl_comments.Person_id = 3
You need to JOIN the tbl_comments and tbl_person tables like so:
tbl_comments.Person_id = tbl_person.id
This is the MySQL select statement I have that's giving me the error - Unknown column 'Regattas.regatta_id' in 'on clause'
SELECT
Regattas.regatta_id,
Events.event_id,
Events.event_name
FROM Regattas, Events
LEFT JOIN Regatta_Events AS Regatta_Events_1 ON Regatta_Events_1.fk_event_id = Events.event_id
LEFT JOIN Regatta_Events AS Regatta_Events_2 ON Regatta_Events_2.fk_regatta_id = Regattas.regatta_id
WHERE Regattas.regatta_id = {$regattaId}
The layout of the tables are like the below:
Regattas Table:
+--------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+--------------+------+-----+---------+----------------+
| regatta_id | int(11) | NO | PRI | NULL | auto_increment |
| regatta_name | varchar(100) | NO | | NULL | |
| regatta_start_date | date | NO | | NULL | |
| regatta_end_date | date | NO | | NULL | |
| regatta_start_time | time | NO | | NULL | |
| regatta_venue_id | int(11) | NO | | 0 | |
+--------------------+--------------+------+-----+---------+----------------+
Events Table:
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| event_id | int(11) | NO | PRI | NULL | auto_increment |
| event_name | varchar(255) | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
And the Regatta_Events table like this - Conjunction table:
+-------------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+---------+------+-----+---------+----------------+
| regatta_events_id | int(11) | NO | PRI | NULL | auto_increment |
| fk_regatta_id | int(11) | NO | | 0 | |
| fk_event_id | int(11) | NO | | 0 | |
+-------------------+---------+------+-----+---------+----------------+
Please Help me fix this I have been on it for a while.
Assuming this is what you're trying to do, you can use UNION ALL for this. You don't need the OUTER JOIN with the first query -- I left it just for reference (not 100% positive what you're trying to achieve):
SELECT
Regattas.regatta_id,
NULL event_id,
NULL event_name
FROM Regattas
LEFT JOIN Regatta_Events ON Regatta_Events.fk_regatta_id = Regattas.regatta_id
WHERE Regattas.regatta_id = {$regattaId}
UNION ALL
SELECT
NULL regatta_id,
Events.event_id,
Events.event_name
FROM Events
LEFT JOIN Regatta_Events ON Regatta_Events.fk_event_id = Events.event_id
I'm not completely sure I understand your desired results. This will return all results from the Events table, and only those results from the Regattas table where the id matches in the input.
Perhaps instead you are looking for something like this:
SELECT
Regattas.regatta_id,
Events.event_id,
Events.event_name
FROM Regattas
LEFT JOIN Regatta_Events ON Regatta_Events.fk_regatta_id=Regattas.regatta_id
LEFT JOIN Events ON Regatta_Events.fk_event_id=Events.event_id
WHERE Regattas.regatta_id = {$regattaId}
I think this should be simple join to return you Regetta_id, event_ID and event_name combination.
SELECT
Regattas.regatta_id,
Events.event_id,
Events.event_name
FROM Regattas as rg
LEFT JOIN Regatta_Events as re ON re.fk_event_id = rg.regatta_id
LEFT JOIN Events AS ev ON re.fk_regatta_id = ev.event_id
WHERE rg.regatta_id = {$regattaId}
I have the following MySQL Table Structure:
mysql> desc customers;
+---------------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+-------------+------+-----+---------+-------+
| hash | varchar(32) | NO | PRI | NULL | |
| date_joined | date | NO | | NULL | |
| agent_code | int(5) | NO | UNI | | |
+---------------------+-------------+------+-----+---------+-------+
mysql> desc persons;
+--------------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+-------------+------+-----+---------+-------+
| agent_code | int(5) | NO | UNI | | |
| team_id | int(2) | YES | | 0 | |
| hash | varchar(32) | NO | PRI | NULL | |
+--------------------+-------------+------+-----+---------+-------+
mysql> desc teams;
+--------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| leader | varchar(32) | NO | UNI | NULL | |
+--------+-------------+------+-----+---------+----------------+
And I'd wish to generate a report of sales by Team.
The SQL Query that I'm using is the following:
SELECT COUNT(`customers`.`agent_code) AS `customer_count`, `teams`.`name`
FROM `customers`
JOIN `persons` ON `customers`.`agent_code` = `persons`.`agent_code`
JOIN `teams` ON `persons`.`team_id` = `teams`.`id`
GROUP BY `teams`.`name`
And it shows the following information:
+----------------+--------+
| customer_count | name |
+----------------+--------+
| 3 | Team 1 |
+----------------+--------+
However I'd like to see the "customer_count" of all the teams in the database, even if the customer_count is null (or zero) for a given team. I have 15 teams in my database, so I'd like to see something like:
+----------------+--------+
| customer_count | name |
+----------------+--------+
| 3 | Team 1 |
| 0 | Team 2 |
| 0 | Team 3 |
| 0 | Team 4 |
+----------------+--------+
I have tried to execute some variants of the following Query, but I always get an error saying that the syntax of OUTER JOIN is incorrect, even though I've read the documentation, and it is correct.
SELECT COUNT( `customers`.`agent_code` ) AS `customer_count` , `teams`.`name`
FROM `customers`
LEFT JOIN `persons` ON `customers`.`agent_code` = `persons`.`agent_code`
LEFT OUTER JOIN `teams` ON `persons`.`team_id` = `teams`.`id`
GROUP BY `teams`.`name`
How can I alter my current query in order to display such result?
You have mistake get main table is person - I suggest your main table is team
SELECT COUNT(`customers`.`id`) AS `customer_count` , `teams`.`name`
FROM `teams`
JOIN `persons` ON `persons`.`team_id` = `teams`.`id`
LEFT JOIN `customers` ON `customers`.`agent_code` = `persons`.`agent_code`
GROUP BY `teams`.`name`
Update: if you do have empty teams, than you need to set left join on persons
SELECT COUNT(`customers`.`id`) AS `customer_count` , `teams`.`name`
FROM `teams`
LEFT JOIN `persons` ON `persons`.`team_id` = `teams`.`id`
LEFT JOIN `customers` ON `customers`.`agent_code` = `persons`.`agent_code`
GROUP BY `teams`.`name`