Select Data from two tables where ID on both tables are same - mysql

Okay I have two tables called subobject: parentID, objectName, subID(primary) and subrelation: ID, className
parentID | objectName | subID ID| className|
_____________________________ ______________
84 | Test | 14 14| BOM
84 | Test2 | 15 15| Schematics
I want to match SubID with ID from both tables depending if they are the same values, then iterate all the values that are the same. Whats the query to do this in Mysql.
this is how I want it to look:
subobjectNAME:
--RelatedClass
--RelatedClass2
etc.
I know this is has something to do with JOIN and this is the mysql Query im using but its not working
"SELECT * from subrelation inner join subobject on subrelation.ID = subobject.subID"
also my while loop to grab this
while($join = mysqli_fetch_assoc($join))

JOIN the two tables:
SELECT
so.objectName,
sr.ClassName
FROM subobject AS so
INNER JOIN subrelation AS sr ON so.subId = sr.ID;
See it in action here:
SQL Fiddle Demo
Also, see the following post for more info about the different types of JOINs:
A Visual Explanation of SQL Joins.

select
a.objectName, b.className
from
subobject a
left join
subrelation b on a.subID = b.ID

Use a Join
SELECT
subobject.ObjectName,
subrelation.ClassName
FROM
subobject
INNER JOIN
subrelation ON subobject.subID = subrelation.ID
You can find information on SQL Joins here:
http://en.wikipedia.org/wiki/Join_(SQL)
And information from the MySQL manual on Joins:
http://dev.mysql.com/doc/refman/5.0/en/join.html

Related

SQL Distinct based on different colum

I have problem to distinct values on column based on other column. The case study is:
Table: List
well | wbore | op|
------------------
wella|wbore_a|op_a|
wella|wbore_a|op_b|
wella|wbore_a|op_b|
wella|wbore_b|op_c|
wella|wbore_b|op_c|
wellb|wbore_g|op_t|
wellb|wbore_g|op_t|
wellb|wbore_h|op_k|
So, I want the output to be appear in different field/column like:
well | total_wbore | total_op
----------------------------
wella | 2 | 3
---------------------------
wellb | 2 | 2
the real study case come from different table but to simplify it I just assume this case happened in 1 table.
The sql query that I tried:
SELECT well.well_name, wellbore.wellbore_name, operation.operation_name, COUNT(*)
FROM well
INNER JOIN wellbore ON wellbore.well_uid = well.well_uid
INNER JOIN operation ON wellbore.well_uid = operation.well_uid
GROUP BY well.well_name,wellbore.wellbore_name
HAVING COUNT(*) > 1
But this query is to calculate the duplicate row which not meet the requirement. Anyone can help?
you need to use count distinct
SELECT
count(distinct wellbore.wellbore_name) as total_wbore
count(distinct operation.operation_name) as total_op
FROM well
INNER JOIN wellbore ON wellbore.well_uid = well.well_uid
INNER JOIN operation ON wellbore.well_uid = operation.well_uid
Final query:
SELECT
well.well_name,
COUNT(DISTINCT wellbore.wellbore_name) AS total_wbore,
COUNT(DISTINCT operation.operation_name) AS total_op
FROM well
INNER JOIN wellbore ON wellbore.well_uid = well.well_uid
INNER JOIN operation ON wellbore.well_uid = operation.well_uid
GROUP BY well.well_name

Stucking with MYSQL to get value from one table to another

this is my first time asking for help. I am not sure if I'm doing it correctly but here's my problem:
Table structure:
Table 'users' Table 'made'
+--+---------+---------+ +--+---------+---------------+
|id|user_id |friends | |id|p_id |result |
+--+---------+---------+ +--+---------+---------------+
|1 |x |["y","z"]| |1 | y |some_text_here |
+--+---------+---------+ +--+---------+---------------+
What I got so far:
SELECT friends FROM users WHERE user_id = 'x'
And loop the information of users.friends in a second query:
SELECT * FROM made WHERE p_id IN ('y', 'z')
What I basicly need is the information of users.friends (from a specific user.user_id) to find if there's a row exiting in "made" which contains users.friends so I can output the made.result data.
I'm using nodeJS to execute the query such as
sql.query("SELECT ....... WHERE user_id = ?", user_id, (err, res) => {......
Hopefully I could explain this good enough, it's pretty much complicated to me.
Thanks for the help in future :)
Okay, from what I can infer, you want the union of data from the two tables right?
You can use the inner property.
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
You can see more here: https://www.w3schools.com/sql/sql_join_inner.asp
One option is to join using JSON function json_search() (available starting MySQL 5.7):
select m.*
from made m
inner join users u on json_search(u.friends, 'one', m.p_id) is not null
where u.user_id = 'x'

Alter columns from selected result table in MYSQL

Here the things,I want to join table 'responsibilities' with fields Name, Direct, Supervise:
Name | Direct | Supervise
ABC 2 4
and table 'positions' with positionCode, positionID:
positionCode | positionID
HR/HRM 2
HR/MN 4
The selected result table will be some thing like this.
Name | Direct | Supervise
ABC HR/HRM HR/MN
The 'Direct' and 'Supervise' column should be positionCode from 'positions' table. Is there an all-in-one query to output this result? Or I have to query 2 times ?
Try this Query,
SELECT r.Name,
p1.positionCode AS Direct,
p2.positionCode AS Supervise
FROM responsibilities r
LEFT JOIN positions p1
ON r.Direct = p1.positionID
LEFT JOIN positions p2
ON r.Supervise = p2.positionID
Output: SEE SQLFiddle DEMO
I think you can join responsibilities twice to the positions table:
SELECT r.Name,
COALESCE(p1.positionCode, 'Direct is N/A') AS Direct,
COALESCE(p2.positionCode, 'Supervise is N/A') AS Supervise
FROM responsibilities r
LEFT JOIN positions p1
ON r.Direct = p1.positionID
LEFT JOIN positions p2
ON r.Supervise = p2.positionID
Follow the link below for a running demo:
SQLFiddle
Try following Query, It should work
select R.Name,(select P.positionCode where R.Direct=P.positionID) as
Direct,(select P.positionCode where R.Supervise=P.positionID) as
Supervise from Responsibilites R, Positions P;
check out this SQL Fiddle for proof that this query works:
http://sqlfiddle.com/#!9/13845c/4/0
Basically, the query looks as follows:
SELECT r.Name, p1.positionCode As Direct, p2.positionCode as Supervise
FROM responsibilities r
LEFT JOIN positions p1 ON r.Direct = p1.positionID
LEFT JOIN positions p2 ON r.Supervise = p2.positionID

MySQL - Join on comma-separated field

I would like to do some new stuff (for me it's new, bc. I'm just a MySQL-beginner) and I did not find a solution for this.
I got these entries in my database:
mytable_items
id | title | catids
1 | test | 32,14
mytable_categories
id | title
32 | Test-Category
14 | Another-Category
Now I would like to join this stuff: Show all data from mytable_items - also show the assigned categories (their titles)
The result should be:
1 | test | Test-Category, Another-Category
How can I solve this?
Thanks a lot in advance :-)
Try this:
SELECT m.id,group_concat(mc.title)
FROM mytable_items m
JOIN mytable_categories mc
ON FIND_IN_SET(mc.id,m.catids)
group by
m.id
SQL FIDDLE DEMO
You should use different entities for each CatID. Then you can join both tables and use Group Concat.
Try this:
Select *
from mytable_items a
join mytable_categories b on a.id = b.id
This will join the data and show it correctly.

Retrieve data from another table

I have a table like
tbl_scripts
script_unique_id | system_id | script_name
+-----------------------------------------+
12345 | 89784 | Demo
And another table goes as
tbl_allowed_group_ids
system_id | script_unique_id |allowed_group_id
+---------------------------------------------+
89784 12345 56987
So now what I want is the row from tbl_scripts with group_id only if the unique id is in allowed_group_id
What I tried is this but is nowhere close to it....I know this is completely wrong
SELECT script_name FROM tbl_scripts WHERE script_unique_id = allowed_group_id
You will want to JOIN the tables:
select *
from tbl_scripts s
left join tbl_allowed_group_ids g
on s.system_id = g.system_id
and s.script_unique_id = g.script_unique_id
See SQL Fiddle with Demo
If you need help learning join syntax, here is a great visual explanation of joins.
A LEFT JOIN will return all rows in the tbl_scripts table regardless of whether or not it has a matching row in the tbl_allowed_group_ids table. If you only want matching rows, then you can use an INNER JOIN
JOIN the two tables:
SELECT t1.script_name
FROM tbl_scripts t1
INNER JOIN tbl_allowed_group_ids t2 ON t1.script_unique_id = t2.allowed_group_id