p.slugUrl AS resultslug, p.id, m.slugUrl, m.categoryParent, pa.pageId, pa.categoryId from tcs_pagecontents as p,tcs_pageassociation as pa, tcs_menucategory_site as m where p.id=pa.pageId and pa.categoryId=m.id ;
This is returning a table with following structure( lets say this table as P)
resultslug
id
slugUrl
categoryParent
pageId
categoryId
Further I wanted to make a join with this resulted table to another table where P.categoryParent = T.id, here T is another table I wanted to fetch the following coloumn name
P.resultslug
P.id
P.slugUrl
P.categoryParent
P.pageId
P.categoryId
T.slugUrl
where P.categoryParent = T.id;
I am getting blank here. Any Idea will be appreciated. Thanks in advance.
Just join that table like the other ones.
select p.slugUrl AS resultslug, p.id, m.slugUrl, m.categoryParent, pa.pageId, pa.categoryId, T.slugURL
from tcs_pagecontents as p
join tcs_pageassociation as pa on p.id=pa.pageId
join tcs_menucategory_site as m on pa.categoryId=m.id
join another_table T on m.categoryParent = T.id;
Related
Now I want to get a comments list, and I have two table named TB_COMMENT, TB_USER;
the TB_COMMENT table has three fields: WORK_ID, USER_ID, ATED_USER_ID;
the TB_USER table has three fields: USER_ID, NICKNAME.
now the front end gives me a workId, and I need to return the list include:
userId, nickname, atedUserId, atedNickname.(and the atedUserId, atedNickname may not exist).
And I just write this sql sentence:
SELECT DISTINCT TB_USER.USER_ID, TB_USER.NICKNAME, TB_COMMENT.ATED_USER_ID
FROM TB_USER, TB_COMMENT
WHERE TB_COMMENT.WORK_ID = #{workId} AND TB_COMMENT.USER_ID = TB_USER.USER_ID`
and I don't know how to get the atedNickname. Hope someone can help me, thanks.
You need one more join with TB_USER on ATED_USER_ID foreign key
SELECT DISTINCT ua.USER_ID, ua.NICKNAME, ub.USER_ID, ub.NICKNAME
FROM TB_USER ua INNER JOIN TB_COMMENT c ON ua.USER_ID = c.USER_ID
LEFT JOIN TB_USER ub ON ub.USER_ID = c.ATED_USER_ID
WHERE c.WORK_ID = #{workId}
I have tables like this:
tbl_product
===========
product_id (PK)
tbl_product_attribute
=====================
pro_attr_id (PK)
pro_attr_pro_id (FK to tbl_product)
pro_attr_attr_opt_id(FK to tbl_attribute_option)
Now, I would like query Products which have 2 attributes in the tbl_product_attribute.
Example like :
SELECT "p".*
FROM "tbl_omx_product" "p"
LEFT JOIN "tbl_omx_product_attribute" "proAttr" ON "proAttr".pro_attr_pro_id = p.product_id
WHERE
(pro_attr_attr_opt_id LIKE '%1759%' ) AND
(pro_attr_attr_opt_id LIKE '%1776%' )
GROUP BY "p"."product_id";
So I'd like to get Products that has exactly 2 values at tbl_omx_product_attribute, which is 1759 & 1776.
But query like above won't show any result unless I use relation OR instead of AND.
The question what is the query to retrieve Products that have 2 values at the tbl_product_attribute ? thank you
Maybe I misunderstood you, but if you want the product to have both attribute options you can query:
SELECT "p".*
FROM "tbl_omx_product" "p"
JOIN "tbl_omx_product_attribute" "proAttr1"
ON "proAttr1".pro_attr_pro_id = p.product_id
AND "proAttr1".pro_attr_attr_opt_id LIKE '%1759%'
JOIN "tbl_omx_product_attribute" "proAttr2"
ON "proAttr2".pro_attr_pro_id = p.product_id
AND "proAttr2".pro_attr_attr_opt_id LIKE '%1776%'
To get products which have two attributes:
SELECT p.*
FROM tbl_omx_product p
LEFT JOIN tbl_omx_product_attribute proAttr ON proAttr.pro_attr_pro_id = p.product_id
GROUP BY p.product_id
having count(*)=2;
You can filter data as per your required output by just adding where clause.
You can try this:
SELECT p.*
FROM tbl_omx_product AS p
LEFT JOIN tbl_omx_product_attribute AS proAttr ON p.product_id = proAttr.pro_attr_pro_id
WHERE
(pro_attr_attr_opt_id LIKE '%1759%' ) AND
(pro_attr_attr_opt_id LIKE '%1776%' )
GROUP BY p.product_id;
Hi I have the following tables and columns.
movie: ID, title
person: ID, name
involved: personID, movieID
I need to answer the question:
"Which movies have either John Travolta or Uma Thurman, but not both starred in?"
I couldn't figure out how to do this without creating new tables, so I made 2 new tables. And tried to do the full outer join on, where you dont get intersecting results. I found out that you can't do full outer joins in mysql but had to do a left join, unioned with a right join. I tried this but don't get the results I wanted at all. I have been stuck for a while now. Can anyone point me in the right direction?
This is what I have so far.
DROP TABLE IF EXISTS Umatable;
DROP TABLE IF EXISTS Johntable;
CREATE TABLE Umatable(title VARCHAR(500));
CREATE TABLE Johntable(title VARCHAR(500));
INSERT INTO Umatable
SELECT m.title
FROM movie m, person p, involved i
WHERE p.name = "Uma Thurman"
AND p.id = i.personid
AND m.id = i.movieiD;
INSERT INTO Johntable
SELECT m.title
FROM movie m, person p, involved i
WHERE p.name = "John Travolta"
AND p.id = i.personid
AND m.id = i.movieiD;
SELECT *
FROM Umatable
LEFT JOIN Johntable ON Umatable.title = Johntable.title
WHERE Johntable.title IS NULL OR Umatable.title IS NULL
UNION
SELECT *
FROM Umatable
RIGHT JOIN Johntable ON Umatable.title = Johntable.title
WHERE Johntable.title IS NULL OR Umatable.title IS NULL
I would do this using aggregation and having:
select i.movieId
from involved i join
person p
on p.id = i.personId
group by i.movieId
having sum(p.name in ('John Travolta', 'Uma Thurman')) = 1;
A count(*) inside a correlated subquery will work:
select *
from movie m
where 1 = (select count(*)
from involved i
join person p
on p.ID = i.personID
and p.name IN ('John Travolta', 'Uma Thurman')
where i.movieID = m.ID)
SQLFiddle Demo
I have three different table as mention below
mRNA_GO
MSU7_LOC
GO_ID
Gene Ontology
GO_ID (Primary Key)
Category
Term
Evidence
miRNA_mRNA
miRNA_ID
MSU7_LOC
Table 1 is connected with table 2 by GO_ID and with table 3 by MSU7_LOC.
I want following columns in my output.
table1.MSU7_LOC
table2.GO_ID
table2.Category
table2.term
table2.Evidence
tabke3.miRNA_ID
I have written two diff query
Query 1
select gene_ontology.go_id , gene_ontology.category, gene_ontology.evidence, gene_ontology.term , mrna_go.MSU7_LOC
from gene_ontology inner join mrna_go on mrna_go.go_id = gene_ontology.go_id
where mrna_go.go_id in ('GO:0009058') ;
Which will give me following columns
table1.MSU7_LOC
table2.GO_ID
table2.Category
table2.term
table2.Evidenc
Query 2
SELECT mrna_go.go_id, mirna_mrna.mirna
from mirna_mrna inner join mrna_go on mrna_go.MSU7_LOC = mirna_mrna.MSU7_LOC
where mrna_go.go_id in ('GO:0009058') ;
which will give me
table2.GO_ID
tabke3.miRNA_ID
Can any one tell me how can I get the output using only one query not two different query..
Just join third table
SELECT
mg.go_id,
mm.mirna ,
g.go_id ,
g.category,
g.evidence,
g.term ,
mg.MSU7_LOC
FROM mirna_mrna mm
inner join mrna_go mg on mg.MSU7_LOC = mm.MSU7_LOC
inner join gene_ontology g on mg.go_id = g.go_id
where mg.go_id in ('GO:0009058') ;
just add a 2nd join in like;
SELECT gene_ontology.go_id , gene_ontology.category, gene_ontology.evidence, gene_ontology.term , mrna_go.MSU7_LOC, mrna_go.go_id, mirna_mrna.mirna
FROM gene_ontology
INNER JOIN mrna_go ON mrna_go.go_id = gene_ontology.go_id
INNER JOIN mirna_mrna ON mrna_go.MSU7_LOC = mirna_mrna.MSU7_LOC
WHERE mrna_go.go_id IN ('GO:0009058') ;
So I have the following table, do_stock_movement, that looks like this:
stock_movement_id sm_number sm_source_id sm_destination_id
15164b86a7533d 145 1516478840ee29 151644d8bd63f2
15166b89d1a9fc 194 15165c481bd9d0 151659e632cd48
The columns sm_source_id and sm_destination_id both reference product points stored in do_product_points.
I'm using the following SQL query:
SELECT * FROM do_stock_movement
INNER JOIN do_product_points ON product_points_id = sm_source_id
WHERE sm_number = '145'
In do_product_points, there's a field called pp_name. I need the corresponding pp_name for both sm_source_id and sm_destination_id.
However, the query above will only return the pp_name for sm_source_id, or for sm_destination_id if you change the joined field to that.
What SQL query will return the corresponding pp_name for both sm_source_id and sm_destination_id?
I hope this is clear. Please ask questions if it isn't!
JOIN this table do_product_points one more times for the sm_destination_id:
SELECT
s.pp_name AS SourcePoint,
d.pp_name AS DestinationPoint,
...
FROM do_stock_movement AS m
INNER JOIN do_product_points s ON s.product_points_id = m.sm_source_id
INNER JOIN do_product_points d ON d.product_points_id = m.sm_destination_id
WHERE m.sm_number = '145'
You need join twice and use alias:
SELECT *, Src.pp_name, Dst.pp_name FROM do_stock_movement
INNER JOIN do_product_points as Src
ON Src.product_points_id = sm_source_id
INNER JOIN do_product_points as Dst
ON Dst.product_points_id = sm_destination_id
You need to join to the product_points table twice, once with source_id and once with destination_id:
SELECT * FROM do_stock_movement move
INNER JOIN do_product_points source ON source.product_points_id = move.sm_source_id
INNER JOIN do_product_points dest ON dest.product_points_id = move.sm_destination_id
WHERE sm_number = '145'