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'
Related
I used for a while Stack, you help me a lot ... this is my first question, I hope to be able to expose correctly.
I need to retrieve all value from a table and add one column with sum value from another table.
This what I mean:
q1=>
SELECT * FROM users WHERE role >30 AND role <50 AND available = 1 AND active = 1
to q1 add a new column from q2=>
SELECT SUM(budget) AS totalBudget FROM projects_assignments
Relation between tables are idUser
more over in projects_assignments I have some users of users' table and some user is in projects_assignments only one time.
example:
table1 = idUser, some values
table2 = idUser, budget, other es: values (1,10000,40),(1,5000,30),(2,5000,30)
My expected result is:
idUser,table1column1,table1column2,totalBudget
1,x,y,15000(10k+5k)
2,x,x,5000
3,x,y,0
...
n,x,y,0
Thanks a lot,
Matteo
select table1.column1,
table1.column2,
table1.column3,
sum(table2.budget) as totalBudget
from table1
inner join table2
on table1.idUser = table2.idUser
where...
group by table1.idUser
I have following query:
http://www.sqlfiddle.com/#!9/752e34/3
This query use SELECT in SELECT queries.
"SELECT a.*
,(SELECT s.value FROM tbl_scd AS s WHERE s.tag_id = 1 AND s.main_id = a.id ORDER BY s.date_time DESC LIMIT 1) AS title
,(SELECT s.value FROM tbl_scd AS s WHERE s.tag_id = 2 AND s.main_id = a.id ORDER BY s.date_time DESC LIMIT 1) AS alt
FROM tbl_main AS a
WHERE 1;"
Now I'm looking for a solution to add a new row into tbl_tag without change the above query (that the SELECT in SELECT part will be dynamic) to get a reference to tbl_tag
To get this:
+----+---------------+-----------+-----------+--------------+
| id | date | title | alt | new_column |
+----+---------------+-----------+-----------+--------------+
| 1 | 2018-10-10 | test1-1 | test1-3 | NULL |
| 2 | 2018-10-11 | test2-1 | test2-1 | NULL |
+----+---------------+-----------+-----------+--------------+
It would be great to get an idea or help.
Thanks
Your last comment on your question about using JOIN makes it clearer to me (I think) what you are after. JOINs will definitely help you a lot here, in place of the rather cumbersome query you are currently using.
Try this:
SELECT
tbl_main.date,
tblA.value AS title,
tblB.value AS alt
FROM
tbl_main
INNER JOIN (SELECT main_id, tag_id, value
FROM tbl_scd
INNER JOIN tbl_tag ON (tbl_scd.tag_id = tbl_tag.id)
WHERE tbl_tag.name = 'title') tblA
ON (tbl_main.id = tblA.main_id)
INNER JOIN (SELECT main_id, tag_id, value
FROM tbl_scd
INNER JOIN tbl_tag ON (tbl_scd.tag_id = tbl_tag.id)
WHERE tbl_tag.name = 'alt') tblB
ON (tbl_main.id = tblB.main_id);
I think this will get you much closer to a general solution to what it looks like you are trying to achieve, or at least point you in a good direction with using JOINs.
I also think you might benefit from re-thinking your database design, because this kind of pivoting rows from one table into columns in a query output can be an indicator that the data might be better off structured differently.
In any case, I hope this helps.
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
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
I have an item master table in MSSQL that has SKUs and all Colors associated with a given SKU:
SKU | Color
-----------
100 | BLK
100 | GRN
101 | RED
101 | BLU
101 | TAN
Then I have a MySql table with a similar structure, but it has Colors (by SKU) that do not exist in the item master table, and I need to delete them.
Here is what I have so far (note I use OPENQUERY to link the two tables):
SELECT * FROM OPENQUERY(WEB, '
SELECT SKU, `Filename`, Color
FROM IP24_Import_Images
') as B
INNER JOIN sap.ItemMasterSkuColor IM
ON B.SKU = IM.Sku
WHERE B.Color NOT IN (
SELECT Color FROM sap.Item_Master_StyleColor
WHERE Sku ....
I'm trying to select them first.. then I should be able to figure out how to delete them from there. But I get stuck at the end there..
Obviously WHERE B.Color != IM.Color will not work. The NOT IN .. I have above would work if I could somehow get the SKU in question from the outer query. How can I get this working properly? Thanks!
Why don't you do somethign like
SELECT *
FROM IP24_Import_Images B
LEFT JOIN sap.ItemMasterSkuColor IM
ON B.SKU = IM.Style AND B.Color = IM.Color
WHERE IM.Color IS NULL
should give you all records from IM and B.color is NULL if it is not matching.
I hope mySQL has the same syntax with right join as this one is MSSQL syntax.