$wpdb->get_results with UNIONs in query - sql

when running each of the following SELECTs on their own (without UNION) I´m getting results as expected. I don´t get any results when using UNION.
Any ideas why this doesn´t work?
$query = "
(SELECT * FROM projects WHERE public='1')
UNION
(SELECT * FROM projects JOIN project_region ON projects.id_project = project_region.id_project
JOIN user ON user.id_region = project_region.id_region WHERE user.user_id = {$current_user->ID})
UNION
(SELECT * FROM projects JOIN project_user ON projects.id_project = project_user.id_project
WHERE project_user.user_id = {$current_user->ID})
";
$projects = $wpdb->get_results($query);
if ($projects) {
foreach ($projects as $project) {
// output results
}
}

In UNION you need to have this same column number, column names for each union query. So in first query you have columns from projects table, but in second query you have columns from projects, project_region, and user tables.

The MYSQL error was *Column 'id_project' in field list is ambiguous*
So the working query looks like this:
$query = "
(SELECT id_project, name FROM projects WHERE public='1')
UNION
(SELECT projects.id_project, projects.name FROM projects JOIN project_region ON projects.id_project = project_region.id_project
JOIN user ON user.id_region = project_region.id_region WHERE user.user_id = {$current_user->ID})
UNION
(SELECT projects.id_project, projects.name FROM projects JOIN project_user ON projects.id_project = project_user.id_project
WHERE project_user.user_id = {$current_user->ID})
";
i found the solution here:
http://sqlzoo.net/howto/source/z.dir/err918/mysql

Related

How i turned postgres query to query builder format codeigniter?

i want to select datas from many tables here is the code
how to turned this
SELECT a.*, b.penyusun, c.keywords
FROM cb_monograf a
INNER JOIN (
select row_number() over (order by id_monograf) nomer, id_monograf, string_agg(id_penyusun::varchar,'-') penyusun
from cb_penyusun_monograf
group by id_monograf
) b ON a.id_monograf = b.id_monograf
INNER JOIN (
select row_number() over (order by id_monograf) nomer, id_monograf, string_agg(id_keywords::varchar,'-') keywords
from cb_keywords_monograf
group by id_monograf
) c ON a.id_monograf = c.id_monograf
WHERE a.jenis = 'buku'
into somekind of this format
$this->db->select('a.*,b.penyusun')
->from('cb_monograf a')
->join("($subquery1) b","a.id_monograf = b.id_monograf","inner")
->where('jenis', $param_type);
You're not far off - try this:
// Just to keep code a bit clearer
$db = $this->db;
// Firstly build the selects
$db->select('row_number() over (order by id_monograf) nomer, id_monograf, string_agg(id_penyusun::varchar,'-') penyusun')
$db->group_by('id_monograf');
$q1 = $db->get_compiled_select('cb_penyusun_monograf');
$db->select('row_number() over (order by id_monograf) nomer, id_monograf, string_agg(id_keywords::varchar,'-') keywords');
$db->group_by('id_monograf');
$q2 = $db->get_compiled_select('cb_keywords_monograf');
// Final query
$db->select('a.*, b.penyusun, c.keywords');
$db->join("($q1) b",'a.id_monograf = b.id_monograf','inner');
$db->join("($q2) c",'a.id_monograf = c.id_monograf','inner');
$db->where('a.jenis','buku');
$data = $db->get('cb_monograf a')->result_array(); // or row_array()
Note this query can be improved if your postgresl supports USING() (and inner can probably be dropped as well) to this:
$db->select('a.*, b.penyusun, c.keywords');
$db->join("($q1) b",'id_monograf'); // inner may also not be required
$db->join("($q2) c",'id_monograf'); // inner may also not be required
$db->where('a.jenis','buku');
$data = $db->get('cb_monograf a')->result_array(); // or row_array()

SQL Server Loop Over Records

I have a view vwDocumentLinks with data as shown below:
I have another table RiskTypes with data as below.
The link between the table & viewis : Idx25 = Risk Type and LinkDocNo = DocumentType. What I am trying to achieve is to get DocumentType in RiskTypes that's not in the view vwDocumentLinks foreach BaseValue and where Idx25 = RiskType. An example using a single BaseValue will be:
SELECT * FROM RiskTypeDocuments WHERE RiskType = 'BUSINESS LIMITED COMPANY' AND DocumentType NOT IN (SELECT LINKDOCNO FROM DBO.VWLINKS WHERE BaseValue = '00007573-1637-4B8E-9374-730AF58BCFB6')
I tried the below query and it's not working as expected. Any help will be greatly appreciated. I am a newbie in SQL
SELECT dbo.RiskTypeDocuments.DocumentType,
dbo.RiskTypeDocuments.RiskType,
dbo.vwLinks.BaseValue AS Document
FROM dbo.vwLinks LEFT OUTER JOIN
dbo.RiskTypeDocuments ON dbo.vwLinks.LinkDocNo =
dbo.RiskTypeDocuments.DocumentType AND dbo.vwLinks.Idx25 =
dbo.RiskTypeDocuments.RiskType
WHERE dbo.RiskTypeDocuments.DocumentType IS NOT NULL
ORDER BY Document
NB: I can't change the schema. I can only create views from the existing tables.
Sample Data from the two datasets: vwDocumentLinks & RiskTypeDocuments
SELECT a.DocumentType,
a.RiskType,
b.BaseValue AS Document
FROM dbo.RiskTypeDocuments as a
INNER JOIN (Select distinct Idx25, BaseValue from vwDocumentLinks) as b on b.Idx25 = a.RiskType
where a.DocumentType not in (Select LINKDOCNO from vwDocumentLinks as c where c.basevalue = b.BaseValue )
I think what you are looking for is NOT EXISTS. It takes a little to get your head around, but the idea is to have what is called a correlated subquery. The subquery is joined to the main query, hence the term correlated.
SELECT *
FROM RiskTypeDocuments
WHERE NOT EXISTS (
SELECT 1 FROM vwDocumentLinks
WHERE RiskTypeDocuments.RiskType = vwDocumentLinks.idx25
AND RiskTypeDocuments.DocumentType = vwDocumentLinks.LinkDocNo
)
If you want to retrieve the records which don't exist in the view then
Try like this
SELECT d.DocumentType,
d.RiskType,
v.BaseValue
FROM dbo.vwLinks AS v
OUTER JOIN dbo.RiskTypeDocuments d ON v.LinkDocNo = d.DocumentType AND v.vwLinks.Idx25 =d.RiskType
WHERE v.BaseValue IS NULL
ORDER BY DocumentType

Comparing Query Result With Table and Retrieve Specific Field

My Query
SELECT
stoMast.sStockistCode,
stoMast.sStateName,
stoMast.sDivision,
stateMap.sRMCode
FROM
tblRSM_State_Mapping stateMap
INNER JOIN
tblStockistMaster stoMast ON
stateMap.sStateName = stoMast.sStateName
WHERE
stateMap.sRMCode = 'MCNE04001'
and
stoMast.sDivision = 'CIDIS'
except
select
sStockistCode,
sStateName,
sDivision,
sRMCode
From
tblEntry
Again I would like to compare the query result columns
sStockistCode
sStateName
sDivision
with tblStockistMaster with the same fields
sStockistCode
sStateName
sDivision
and retrieve the STOCKIST NAME.
Don't know how to compare the above query result with the table.
Maybe you can use following SQL code used with CTE expression
;with cte as (
SELECT
stoMast.sStockistCode,
stoMast.sStateName,
stoMast.sDivision,
stateMap.sRMCode
FROM
tblRSM_State_Mapping stateMap
INNER JOIN
tblStockistMaster stoMast ON
stateMap.sStateName = stoMast.sStateName
WHERE
stateMap.sRMCode = 'MCNE04001'
and
stoMast.sDivision = 'CIDIS'
except
select
sStockistCode,
sStateName,
sDivision,
sRMCode
From
tblEntry
)
select
cte.*,
sm.sStockistName
from cte
left join tblStockistMaster as sm
on sm.sStockistCode = cte.sStockistCode and
sm.sStateName = cte.sStateName and
sm.sDivision = cte.sDivision
-- I retrieve the stockist Name
SELECT
stoMast.sStockistName,
FROM
tblRSM_State_Mapping stateMap
INNER JOIN
tblStockistMaster stoMast
ON stateMap.sStateName = stoMast.sStateName
-- I'm joining table entry If I can match
LEFT JOIN
tblEntry tbl
on tbl.sStockistCode = stoMast.sStockistName
AND tbl.sStateName = stoMast.sStateName
AND tbl.sDivision = stoMast.sDivision
AND tbl.sRMCode = stateMap.sRMCode
WHERE
stateMap.sRMCode = 'MCNE04001'
and stoMast.sDivision = 'CIDIS'
-- And The the exept thing, I don't want that got a match with the tableentry
AND COALESCE(tbl.sStockistCode, tbl.sStateName, tbl.sDivision, tbl.sRMCode) is null
I expect it is what you wanted to get. On another way please help us understand wht you come from (tables and idea of what data can be in) and the result you want. Will be easier to create a query.

I need to do a sub-select in DQL

I have this query:
$query = $this->createQueryBuilder('l')
->select('l')
->leftJoin('l.processedLeads', 'pl')
->where('pl.company <> :company')
->andWhere('pl.company IS NULL')
->setParameters(array('company' => $company))
->getQuery();
But i need it formed like the following:
SELECT * FROM leads WHERE NOT IN
( SELECT * FROM processed_leads WHERE lead = :lead AND company = :company)
Can i do a sub-select in a join where 2 parameters of the join (lead_id and company) do not exist?
As in, only select the leads that do not exist in processedLeads with the specific company?
I'm not familiar with the QueryBuilder, but I would recommend something like this in DQL:
$query = $em->createQuery('SELECT l FROM Project\model\leads l WHERE l NOT IN
(SELECT p.lead FROM Project\model\processed_leads p WHERE p.lead = ? AND company = ?)')
->setParameters(array($lead, $company))
->getResult;
Hopefully, this helps you. I would use the NOT INstatement. In case the sub query returns a null value, there is just no lead in the processed_leads for this company and you'll get all leads - which should be ok in this case.

Linq to sql - Join 2 tables, select 1 row from right table with a 1 to many relationship

I am trying to select the forum categories and the last post in each respective category. I have been able to accomplish this in SQL using OUTER APPLY, but haven't been able to successfully convert this to LINQ.
Produces the desired result in SQL:
SELECT fc.CategoryID, fc.CategoryName, fc.PostCount, ft.Title, ft.LastPost, ft.LastPostId, ft.TopicId
FROM ForumCategory AS fc
OUTER APPLY
(
SELECT TOP (1) *
FROM ForumTopic
WHERE ForumTopic.CategoryID = fc.CategoryID
ORDER BY ForumTopic.lastpost DESC
) ft
My attempt at converting to LINQ:
Dim query = From fc In ctx.ForumCategories _
Join ft In ctx.ForumTopics On fc.CategoryID Equals ft.CategoryID _
Select New With {
fc.CategoryID,
fc.CategoryName,
fc.PostCount,
ft.Title,
ft.LastPostId,
ft.LastPost.OrderByDescending().First()
}
I am getting an error stating: 'OrderByDescending' is not a member of 'Date?'
You write it as follows:
var q = from fc in ctx.ForumCategories
from ft in (from x in ctx.ForumTopic
where x.CategoryID == fc.CategoryID
order by x.lastpost desc
select new
{
x.Title,
x.LastPost,
x.LastPostId,
x.TopicId
}).Take(1).DefaultIfEmpty()
select new
{
fc.CategoryID,
fc.CategoryName,
fc.PostCount,
ft.Title,
ft.LastPost,
ft.LastPostId,
ft.TopicId
};