how to index a many to many relation by sphinx - join

I have a question about index a many to many relation.
e.g:
table A : id,value_a(string)
table B : id,value_b(string)
table C: id_a,id.b,value_c(string)
the query is :
select * from C where C.id_a = A.id And A.value_a like keyword_a
and C.id_b = B.id andB.value_b like keyword_b
so i want to index table A and table B with full-text search by sphinx,but I can not find
a method to join two
search result to table C .
can anyone help me ? thanks a lot!

You need to make an index with all the data
sql_query = select C.id,id_a,id_b,value_a,value_b,value_c \
from C \
inner join A on (C.id_a = A.id) \
inner join B on (C.id_b = B.id)
sql_attr_uint = id_a
sql_attr_uint = id_b
by storing id_a and id_b in the index as attributes, can group by (at search time) to only get one document per A or B.
btw, a query would be something like
$cl->setMatchMode(SPH_MATCH_EXTENDED);
$res = $cl->Query('#value_a keyword_a #value_b keyword_b',$sphinx_index);

Related

select all records from left table that do not exist in right table

I am working on sql server I have two tables and I need to return records from the left table which are not found in the right table for that I am using left join like below query,
select #MID=MID,#MName=Name,#PID=PID,#PName=PName,#DID=DID from #CompanyDataInfo where id=#MCount
insert into #temp SELECT Top(1) f.Name,f.PID,f.PName,v.* FROM #CompanyDataInfo f
left join Employee v on v.Id=f.ID and v.DID=f.DID
where v.Id =#MID and v.DId = #DId and v.PId = #PId and v.CId =#CId and DATE_TIME between DATEADD(minute,-555,GETDATE()) and GETDATE() order by DATE_TIME desc
Result should be all rows from #CompanyDataInfo table while no record found in Employee table for related ID, I googled and use "v.Id is null" but not getting expected result
Is there any solution greatly appriciable
Thanks In advance
Your query is not using left join in correct way. You are using your right table reference in where clause. I try to correct it below but I don't have full information about your table schema. Please try this-
select
#MID = MID,
#MName = Name,
#PID = PID,
#PName = PName,
#DID = DID
from #CompanyDataInfo
where id = #MCount
insert into #temp
select
f.Name,
f.PID,
f.PName,
v.*
from #CompanyDataInfo f
left join Employee v on v.Id=f.ID and v.DID=f.DID
where f.Id = #MID and
f.DId = #DId and
f.PId = #PId and
f.CId = #CId and
f.DATE_TIME between DATEADD(minute,-555,GETDATE()) and GETDATE() and
v.Id is null
order by f.DATE_TIME desc
Add ...and v.Id is null to your where clause.

join querybuilder ax 2012 x++

Excuse my english (is not my native language)
Well, i wanna make this query (i make the temp table EMPL, the first temp table VEN is very easy but i don't see how to make the join)
SELECT 'CADORE_MP',EMPL.PERSONNELNUMBER, EMPL.NOMBRE, 0 AS CANTIDAD, VEN.MONTO FROM
(
SELECT VATNUM, SUM(INVOICEAMOUNT) AS MONTO FROM CUSTINVOICEJOUR
WHERE INVOICEDATE>=#FECHAI and INVOICEDATE<=#FECHAF
AND PAYMENT LIKE '%DIAS%'
AND CUSTGROUP LIKE 'EMP%'
AND REVERSE_GT=0 AND TAXTYPEDOCUMENTID='FC'
GROUP BY VATNUM) VEN
INNER JOIN
(
SELECT HW.PERSONNELNUMBER, HPIN.IDENTIFICATIONNUMBER, PRE.FIRSTNAME+' '+PRE.SECONDNAME+' '+PRE.FIRSTLASTNAME+' '+PRE.SECONDLASTNAME AS NOMBRE
FROM HcmPersonIdentificationNumber HPIN INNER JOIN HCMWORKER HW ON HPIN.PERSON=HW.PERSON AND HPIN.IDENTIFICATIONTYPE=5637146829
INNER JOIN PAYROLLEMPL PRE ON HW.PERSON=PRE.PERSON
INNER JOIN HCMEMPLOYMENT HE ON HW.RECID=HE.WORKER
LEFT JOIN PAYROLLLIQUIDATION PL ON HW.PERSONNELNUMBER=PL.EMPLID AND PL.DATELOW<#FECHAF
LEFT JOIN PAYROLLGROUPEMPLOYEES PRGE ON HW.PERSONNELNUMBER=PRGE.EMPLID AND GROUPSEMPLYEESID='CADORE_PQ'
WHERE HE.VALIDTO>='21541231' AND PL.EMPLID IS NULL AND PRGE.EMPLID IS NULL) EMPL ON VEN.VATNUM=EMPL.IDENTIFICATIONNUMBER
it have some parameters and other are constants.
so, i can do the two inner selects but later i can't do the join of this two temp tables
and i wanna know if someone can explain me the diference of join, exists join, no exists join. something like this
and if is posible to make when only wanna data from A but do not intersect with B
This is what i have
Query query;
QueryRun Run;
QueryBuildDataSource dataSourceHW;
QueryBuildDataSource dataSourceHPIN;
QueryBuildDataSource dataSourcePRE;
QueryBuildDataSource dataSourceHE;
QueryBuildDataSource dataSourcePL;
QueryBuildDataSource dataSourcePRGE;
str textDesc = "";
date FechaF;
FechaF = str2Date('21/03/2016',123);
query = new Query();
dataSourceHW = query.addDataSource(tableNum(HcmWorker));
dataSourceHPIN = dataSourceHW.addDataSource(tableNum(HcmPersonIdentificationNumber));
dataSourceHPIN.addLink(fieldNum(HcmWorker, Person), fieldNum(HcmPersonIdentificationNumber, Person));
dataSourceHPIN.addRange(fieldnum(HcmPersonIdentificationNumber,IdentificationType)).value('5637146829');
dataSourceHPIN.fetchMode(QueryFetchMode::One2One);
dataSourceHPIN.joinMode(JoinMode::InnerJoin);
dataSourcePRE = dataSourceHW.addDataSource(tableNum(PayRollEmpl), "PayRollEmpl");
dataSourcePRE.addLink(fieldNum(HcmWorker, Person), fieldNum(PayRollEmpl, Person));
dataSourcePRE.fetchMode(QueryFetchMode::One2One);
dataSourcePRE.joinMode(JoinMode::InnerJoin);
dataSourceHE = dataSourceHW.addDataSource(tableNum(HcmEmployment), "HcmEmployment");
dataSourceHE.addLink(fieldNum(HcmWorker, RecId), fieldNum(HcmEmployment, Worker));
dataSourceHE.addRange(fieldnum(HcmEmployment,ValidTo)).value(strFmt('%1' ,DateTimeUtil::maxValue()));
dataSourceHE.fetchMode(QueryFetchMode::One2One);
dataSourceHE.joinMode(JoinMode::InnerJoin);
dataSourcePL = dataSourceHW.addDataSource(tableNum(PayRollLiquidation));
dataSourcePL.addLink(fieldNum(HcmWorker, PersonnelNumber), fieldNum(PayRollLiquidation, EmplId));
dataSourcePL.addRange(fieldnum(PayRollLiquidation,DateLow)).value('21/03/2016');
dataSourcePL.fetchMode(QueryFetchMode::One2One);
dataSourcePL.joinMode(JoinMode::OuterJoin);
// this join under this dont work, indistinctly if i use it, always show me the same
// because of this i ask above about the data of table A only
dataSourcePRGE = dataSourceHW.addDataSource(tableNum(PayRollGroupEmployees));
dataSourcePRGE.addLink(fieldNum(HcmWorker, PersonnelNumber), fieldNum(PayRollGroupEmployees, EmplId));
dataSourcePRGE.addRange(fieldnum(PayRollGroupEmployees,GroupsEmplyeesId)).value('CADORE_PQ');
//dataSourcePRGE.addRange(fieldnum(PayRollGroupEmployees,EmplId)).value(SysQuery::valueEmptyString());
dataSourcePRGE.fetchMode(QueryFetchMode::One2One);
dataSourcePRGE.joinMode(JoinMode::NoExistsJoin);
will thanks you if someone help me

need to convert SQL CROSS JOIN into LINQ

I have two tables table1 and table2. Each table contains a column with itemPrice. I need to add the two columns together.
The SQL query below returns the correct SUM.
SELECT SUM(item1+ item2) FROM
(select SUM(t1.itemPrice) item1 from table1 t1 WHERE t1.userid=='jonh') tableA
CROSS JOIN
(select SUM(t2.itemPrice) item2 from table2 t2 WHERE t1.userid=='jonh') tableB
I am not been lazy but the above query has so many SUM functions that I don't know where to start to write LINQ queries.
Can anyone help?
Ceci,
Hopefully this will give you what you want...
from f in (
from x in ( from t1 in Table1
where t1.Userid.Equals("John")
select new { Userid = t1.Userid }
).Distinct()
select new { item1 = ( from z in Table1
where z.Userid.Equals("John")
select z.ItemPrice ).Sum() ??0 ,
item2 = ( from z in Table2
where z.Userid.Equals("John")
select z.ItemPrice ).Sum() ??0 }
) select new { total = f.item1 + f.item2 }
In the case where there are no records for "john" in one table, it will bring back a 0 and sum up the other tables.
hope this helps.

JPQL join tables doubles column names

I have multiple tables in a join and every table has a column ID. So in the resultig join there are a lot of ID columns. How I can access a specific ID column with the criteria API?
ParameterExpression<A> idParam = criteriaBuilder.parameter(A.class, PARAM_NAME);
Subquery<B> sq = query.subquery(B.class);
Root<B> root = sq.from(B.class);
Join<C, D> joinTogether = root.join("memberX").join("memberY");
sq.select(root);
sq.where(criteriaBuilder.and(criteriaBuilder.equal(joinTogether.get("id"), idParam), criteriaBuilder.equal(parentQuery.get("id"), root.get("id"))));
The problem is, that in the resulting SQL contains
SELECT 1 FROM E t6, B t5, C t4, D t3 WHERE ((( = paramName) AND (t0.ID = t5.ID)) AND (((t6.memberZ = t5.ID) AND (t4.ID = t6.memberX)) AND (t3.ID = t4.memberY))))
The table E (t6) is an additional join table between table B and C, t0 is the reference to the parent query. Instead t3.id = :paramName EclipseLink creates nothing just before the first equal-sign (paramName is the content of the constant PARAM_NAME). My idea is, that the "id" column could reference all tables and EclipseLink can not decide, which table I mean.
How I can change that?
Thank you
André

Nested query in Linq

I have three tables. I have to retrieve the data using Linq statement. My three table names are A,B,C. I connected join for connecting two tables A and B based on the id's like:
select ol, fN, LN, ci, co
from member
join details
on member_id = details_id
where details_id in
(select contacts_id from contacts where
contacts_id1 = 1 and contacts_usr_id = 1)
I am able to write the query up to the where condition, how can I write the query for the inner while condition?
you can try this:
var idlist = (from tbl in table3
where tbl.usr_id == 1 && tbl.contacts_id == 1
select tbl.contacts_id ).ToList();
var x = from A in table1
from B in table2 where A.user_id == B.user_id
&& idlist.Contains(A.user_id)
select new { a = A.a, b = A.b, c = A.c, d = B.d, e = B.e };
check and let me know if that work.
Try flipping the query upside down. How about the following:
var query =
from t3 in table3
where t3.user_id = 1 && t3.contacts_id = 1
join t2 in table2 on t3.contacts_id equals t2.usr_id
join t1 in table1 on t2.usr_id equals t1.userid
select new {t2.a, t2.b, t2.c, t1.d, t1.e};

Resources