I want to select a table which holds all the issues a specific project id has. For that I am planning to do a LEFT JOIN and get the projects_id in Projects table to the column 'issues_projects_id' in issues table. Isn't it a JOIN that has to be used here?
// $this->db->join('issues', 'issues.issues_projects_id = projects.projects_id', 'left');
// $query1 = $this->db->get();
$squery="SELECT * FROM projects LEFT JOIN issues ON issues.issues_projects_id=projects.projects_id";
$query1= $this->db->query($squery);
$query= $this->db->query($squery);
return $query->result();
return $query1->result();
The above code shows below error & displays no output.
A Database Error Occurred
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'LEFT JOIN `issues` ON `issues`.`issues_projects_id` = `projects`.`projects_id`' at line 2
SELECT * LEFT JOIN `issues` ON `issues`.`issues_projects_id` = `projects`.`projects_id`
Filename: C:/wamp64/www/projects/issue_tracker/system/database/DB_driver.php
Line Number: 691
Projects Table
projects_id
projects_name
1
abc
2
xyz
Issues Table
issues_id
issues_description
issues_projects_id
1
gfj
1
2
fghgj
1
you are missing from statement:
SELECT *
FROM `projects` --< here
LEFT JOIN `issues`
ON `issues`.`issues_projects_id` = `projects`.`projects_id`
WHERE projects_id = ?
It seems you are self-joinin the table issue, doing that will no give you all the data that you seem to require.
Also a best practice is to NOT use select * but instead write all the required column name.
SELECT `Projects_id`, `projects_name`, `issues_id`, `issues_description`, `issues_projects_id`
from `projects`
LEFT JOIN `issues` ON `issues`.`issues_projects_id` = `projects`.`projects_id`
Related
I have two tables that I'm trying to connect. One table is called 2019projections and the other is called 2019actualstat. I want to connect the two by names. I'm 99% sure every name that is in 2019actualstat is in 2019projections, but not every name in 2019actualstat is in 2019projections. The latter has alot more names, but most of them are useless.
I've tried left join and right join.
I've tried select distinct
I gave a shot at exists
This is what I have so far:
USE Fantasyfootball;
SELECT DISTINCT *
FROM 2019actualstat;
LEFT JOIN 2019projections ON
2019actualstat.Player =
2019projections.first_last;
It's giving me the 1064 error, but I think it has to do with the 2019projections table having more records.
21:27:26 LEFT JOIN 2019projections ON 2019actualstat.Player =
2019projections.first_last Error Code: 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 'LEFT JOIN 2019projections ON
2019actualstat.Player = 2019projections.first_last' at line 1 0.00071
sec
2019projections.first_last is a varchar(50) and 2019actualstat.player is text
PS: I have the .csv files which I'm not sure how to post, but I would be happy to send them both.
You're missing the select list, and have a redundant (wrong) semicolon at the end of the from clause:
SELECT *
FROM 2019actualstat
LEFT JOIN 2019projections ON 2019actualstat.Player = 2019projections.first_last;
Good Afternoon,
I am trying to update a table using price data from another table however I get an error using an inner join. I am sure its something very stupid but having spent the best part of my day on this its time to ask for help.
If I do the following SELECT statement to test my inner join syntax works as it should
SELECT *
FROM polaracc_osrs_property_field_value
INNER JOIN polaracc_osrs_properties
ON polaracc_osrs_property_field_value.pro_id = polaracc_osrs_properties.id
WHERE polaracc_osrs_property_field_value.field_id =112
However when I then try and run an update statement using the price from one table to populate the 2nd I get the below error
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 'FROM polaracc_osrs_property_field_value INNER JOIN
polaracc_osrs_properties ' at line 3
The syntax used for the update statement is below
UPDATE polaracc_osrs_property_field_value
SET polaracc_osrs_property_field_value.value_integer = polaracc_osrs_properties.price
FROM polaracc_osrs_property_field_value
INNER JOIN polaracc_osrs_properties
ON polaracc_osrs_property_field_value.pro_id = polaracc_osrs_properties.id
WHERE polaracc_osrs_property_field_value.field_id = 112
Your join needs to happen before you set your values like this:
UPDATE polaracc_osrs_property_field_value
INNER JOIN polaracc_osrs_properties
ON polaracc_osrs_property_field_value.pro_id = polaracc_osrs_properties.id
SET polaracc_osrs_property_field_value.value_integer = polaracc_osrs_properties.price
WHERE polaracc_osrs_property_field_value.field_id = 112;
Hope this helps.
I have branch table , inventory table and item table. I want to retrieve item id, item name , qty (quantity) and branch name (branch_add).
SELECT tbl_item.item_name ,
tbl_inventory. tbl_item_item_ID , tbl_inventory.qty , tbl_branch.branch_add
FROM tbl_item,tbl_inventory ,tbl_branch
WHERE (tbl_inventory.tbl_item_item_ID = tbl_item.item_ID) JOIN (tbl_branch.branch_ID=tbl_inventory`.tbl_branch_branch_ID)
That is the code which I have wrote but it gives bellow error.
ERROR 1064 (42000): 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 'join item_ID.tbl_item =
tbl_item_item_ID.tbl_inventory' at line 3
Could anyone please help me to solve this. And table will displayed on attached png.
You have a missing AND between your WHERE conditions:
select
tbl_branch_branch_ID.tbl_inventory
, item_name
from tbl_inventory
, tbl_item
, tbl_branch
where
branch_ID.tbl_branch=tbl_branch_branch_ID.tbl_i
and item_ID.tbl_item = tbl_item_item_ID.tbl_inventory;
Also, your JOIN conditions are a little bit off.
I think this is the query you're actually looking for:
select
t_inv.*
, t_itm.item_name
from tbl_inventory t_inv
inner join tbl_item t_itm on t_inv.t_item_item_id = t_itm.item_id
inner join tbl_branch t_br on t_br.branch_id = t_inv.tbl_branch_branch_id
Using explicit JOINs is much better and clearer for reading the code and also can result in improved code performance.
SELECT
tbl_item.item_name,
tbl_inventory.tbl_item_item_ID,
tbl_inventory.qty,
tbl_branch.branch_add
FROM
tbl_item,
tbl_inventory,
tbl_branch
WHERE
(
inner join tbl_item ON tbl_inventory.tbl_item_item_ID = tbl_item.item_ID
inner join branch_ID ON tbl_branch.branch_ID = tbl_inventory.tbl_branch_branch_ID
);
The above query gives syntax error.
I'm learning SQL and have built up a little database so far. Now I have 2 tables called countries_visited and regions_visited with the following schema,
countries_visited: [id,countries,year_visited]
regions_visited: [r_id,regions,c_id,month_visited]
The id column in countries_visited is a primary key, and the c_id column of regions_visited is a foreign key linked to countries_visited
The month_visited column in regions_visited is newly created and all entries are now NULL. What I want to do is to individually insert the entry into this new column with conditions.
So, I tried the following statement taking from the earlier examples here in StackOverflow,
UPDATE regions_visited
SET month_visited = 9
FROM countries_visited
JOIN regions_visited ON countries_visited.id = regions_visited.c_id
WHERE regions_visited.regions = 'Seoul'
AND countries_visited.year_visited = 2006
AND countries_visited.countries = 'South Korea';
By running this command on MySQL server, it shows this error:
ERROR 1064 (42000): 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 'FROM regions_visited INNER JOIN countries_visited
ON regions_visited.c_id = coun' at line 1
However, if neglecting the UPDATE and SET part of the statement, mysql shows the joined table with only 1 row that I would like to update. What did I do wrong? How to make this work?
Thank you in advance!
In MySQL, the join clauses come before the setclause:
UPDATE regions_visited rv
JOIN countries_visited cv ON ON cv.id = rv.c_id
SET month_visited = 9
WHERE rv.regions = 'Seoul' AND
cv.year_visited = 2006 AND
cv.countries = 'South Korea';
Move the Join before SET. Try this
UPDATE regions_visited
JOIN regions_visited
ON countries_visited.id = regions_visited.c_id
SET month_visited = 9
WHERE regions_visited.regions = 'Seoul'
AND countries_visited.year_visited = 2006
AND countries_visited.countries = 'South Korea';
EDIT: I am using phpMyAdmin interface, and I have been copy/paste the codes from phpMyAdmin to here. The phpMyAdmin seems to run a "different code" as I run the following code, and generating some error message that are referring to that "different code", causing huge confusion.
** Final edit: It seems Safari is causing this: it run the "different query" when I try to run 2nd query below. Use Firefox instead, and it generate correct results. Thanks for the help and sorry for the confusion. **
I have two tables: newsFeeds, comments, where
** newsFeeds contains column PID, comments contains column FID.**
I want to join rows in two tables with matching PID = FID. My code is:
SELECT * FROM newsFeeds
INNER JOIN
(
SELECT * FROM
comments
)
ON comments.FID = newsFeeds.PID
and the error message is "#1248 - Every derived table must have its own alias".
I then add in AS newtb after ) according to other posts here. And the code is then:
SELECT * FROM newsFeeds
INNER JOIN
(
SELECT * FROM
comments
) AS newtb
ON newtb.FID = newsFeeds.PID
But another error shows up: #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 'INNER JOIN( SELECTFID,COUNT(*) AScount FROMcomments
LIMIT 0, 25' at line 8
I wonder how to correctly do this?
You should correct this by removing the derived table:
SELECT *
FROM tb_1 INNER JOIN
tb_2
ON tb_2.FID = tb_1.PID;
MySQL has a tendency to materialize derived tables, which hurts performance.
The answer to your question, though, is to add a name after the parentheses:
SELECT *
FROM tb_1 INNER JOIN
(SELECT *
FROM tb_2
) t2
ON t2.FID = tb_1.PID;