SQL query to retrieve data from 3 tables - mysql

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.

Related

LEFT JOIN in Codeigniter shows syntax error

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`

MySQL trouble connecting two columns of names

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;

How to do two different count in one sql

How can do two different count in one table?
The two different count functions count different columns.
Simplified Table:
id,creatorId,resolverId
'1','1','2'
'2','1','2'
'3','2','2'
'4','2','1'
What I want to do is putting the creatorId,COUNT(creatorId),resolverId,COUNT(resolverId) into one table. Like:
creatorId,COUNT(creatorId),resolverId,COUNT(resolverId)
'1','2','1','1'
'2','2','2','3'
I only passed the test of putting them in 2 columns by using UNION, and I tried JOIN but it is illegal to MySQL.
SELECT creatorId, COUNT(creatorId)
FROM issue AS a
GROUP BY creatorId
join(
SELECT resolverId, COUNT(resolverId)
FROM issue AS b
GROUP BY resolverId)
WHERE a.creatorId = b.resolverId;
The error info is:
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 'join( SELECT resolverId, COUNT(resolverId) FROM issue AS b GROUP BY resolverId)' at line 4 0.00034 sec
Can anyone tell me how to deal with it? or give me a example?
Thank you!
select a.creatorId,COUNT(a.creatorId), t.resolverId, count_resolved_id
from issue a
inner join (
SELECT b.resolverId, COUNT(b.resolverId) count_resolved_id
FROM issue AS b
GROUP BY resolverId
) t on t.resolverId = a.creatorId
group by a.creatorId;
you could jon this way
select a.creatorId,COUNT(acreatorId), resolverId, count_resolved_id
from issue a
inner join (
SELECT resolverId, COUNT(resolverId) count_resolved_id
FROM issue AS b
GROUP BY resolverId
) t on t.resolverId = a.creatorId
group by a.creatorId,COUNT(acreatorId)
If I understand correctly, then one way of doing this is an aggregation after a union all:
select id, sum(creator) as creator_cnt, sum(resolver) as resolver_cnt
from ((select creator_id as id, 1 as creator, 0 as resolver
from issue
) union all
(select resolver_id as id, 0 as creator, 1 as resolver
from issue
)
) cr
group by id;

Joining derived tables

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;

Can I count number of rows in joined table?

Like this:
SELECT s.*, count( logs.* ) as ssh_count
FROM servers s
LEFT JOIN logs ON s.ip_address = logs.server_ip
But I get an error with that query:
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 ssh_count FROM servers s LEFT JOIN logs ON s.ip_address = logs.server_ip LIMIT' at line 1
I think that's because you can't address a table in the count function.
I can do this using a subquery, but that will likely slowly down the query.
What is a better way of doing this?
You can adress a table column, but you can't address table.*, you can do this for example:
SELECT s.*, count( logs.server_ip ) as ssh_count
FROM servers s
LEFT JOIN logs ON s.ip_address = logs.server_ip