i have 3 tables.
tenant
tenant_id : int
category_id : int
category
category_id : int
category_name : varchar(50)
history
tenant_id : int
bulan_tahun : varchar(8)
counter : int
i want to join all of this table using this code:
SELECT a.tenant_id, a.category_name
FROM (
(tenant INNER JOIN category ON tenant.category_id = category.category_id) AS a
INNER JOIN (
SELECT tenant_id, counter FROM history WHERE
bulan_tahun = DATE_FORMAT(CURDATE(), '%m_%Y')
) AS b
on a.tenant_id = b.tenant_id
)
but this code produce an error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'a INNER JOIN ( SELECT tenant_id, counter FROM history WHERE bulan_tahun ' at line 3
if i separate the sub select, it works perfectly
for the first select:
SELECT tenant_id, category_name
FROM
(tenant INNER JOIN category ON tenant.category_id = category.category_id)
and the second select:
SELECT tenant_id, counter FROM history WHERE
bulan_tahun = DATE_FORMAT(CURDATE(), '%m_%Y')
but if i join this together, the error occured
can anyone help me?
Problem lies here..
(tenant INNER JOIN category ON tenant.category_id = category.category_id)
Try restructuring your entire query like this
SELECT a.tenant_id, c.category_name
FROM
tenant t
INNER JOIN
category c
ON ( t.category_id = c.category_id )
INNER JOIN
history h
ON ( t.tenant_id = h.tenant_id )
WHERE
h.bulan_tahun = DATE_FORMAT(CURDATE(), '%m_%Y')
Related
I have bd hf3 and 5 tables there:
active_preset with columns (id , preset_id)
preset with columns (id , birja_id, trend_id, fractal, interval_up)
birja with columns (id , name)
trend with columns (id , name)
uq_active_preset with columns (id , birja, trend, fractal, interval_up)
In table preset I have a few records. Some of them are in table active_preset by foreign key preset_id. In table active_preset a few records exist once , a few more than once.
I need to update table uq_active_preset with records from table active_preset disregarding repetitions of records if they are present.
I did query from active_preset and it works good:
SELECT
b.name AS birja, p.fractal AS fractal , tre.name AS trend, p.interval_up AS interval_up
FROM hf3.active_preset AS ap
INNER JOIN hf3.preset AS p on p.id = ap.preset_id
INNER JOIN hf3.birja AS b on b.id = p.birja_id
INNER JOIN hf3.trend AS tre on tre.id = p.trend_id
GROUP BY b.name, p.fractal, tre.name, p.interval_up
HAVING COUNT(*) >= 1
But I don't know how to update uq_active_preset
I tried this and it returns syntax error:1064 :
UPDATE hf3.uq_active_preset uap SET
uap.birja = st.birja ,
uap.fractal = st.fractal,
uap.trend = st.trend,
uap.interval_up = st.interval_up,
FROM (SELECT b.name AS birja, p.fractal AS fractal , tre.name AS trend, p.interval_up AS interval_up
from hf3.active_preset AS ap
INNER JOIN hf3.preset AS p on p.id = ap.preset_id
INNER JOIN hf3.birja AS b on b.id = p.birja_id
INNER JOIN hf3.trend AS tre on tre.id = p.trend_id
GROUP BY b.name, p.fractal, tre.name, p.interval_up
HAVING COUNT(*) >= 1
) st
when you make an update using from is like you join the updated table with your query result. So, you need also a where statement in order to tell where those two are connected. Also, don't use alias of your updated table on set statement.
You need something like that:
UPDATE hf3.uq_active_preset uap SET birja=st.birja,fractal=st.fractal,trend=st.trend,interval_up=st.interval_up
FROM (SELECT b.name AS birja, p.fractal AS fractal , tre.name AS trend, p.interval_up AS interval_up
from hf3.active_preset AS ap
INNER JOIN hf3.preset AS p on p.id = ap.preset_id
INNER JOIN hf3.birja AS b on b.id = p.birja_id
INNER JOIN hf3.trend AS tre on tre.id = p.trend_id
GROUP BY b.name, p.fractal, tre.name, p.interval_up
HAVING COUNT(*) >= 1
) st
where uap.fkey=st.fkey
I want to use data of view in WHERE clause. But getting an error:
create view post_with_answers AS
SELECT DISTINCT postid
FROM (SELECT postid FROM `qa_posts` WHERE `type` = 'Q') AS q1
INNER JOIN (SELECT parentid FROM `qa_posts` WHERE `type` = 'A') AS q2 ON q1.postid = q2.parentid
select count(*)
from qa_posts
where parentid not in post_with_answers
On the last row I am getting this error:
SQL Error [1064] [42000]: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'post_with_answers' at line 3
How to fix that?
Just like you would use a table:
select count(*)
from qa_posts
where parentid not in (select pwa.postid from post_with_answers pwa);
I would caution you from using not in with a subquery. No rows are returned if even one value from the subquery is NULL. For this reason, I recommend NOT EXISTS:
select count(*)
from qa_posts p
where not exists (select 1
from post_with_answers pwa
where p.parentid = pwa.postid
);
In addition, your view definition is a bit over complicated. You don't need subqueries:
create view post_with_answers AS
SELECT DISTINCT pq.postid
FROM qa_posts pq JOIN
qa_posts pa
ON pq.postid = pa.parentid
WHERE pq.type = 'Q' AND pa.type = 'A';
Then, the DISTINCT just adds overhead, so EXISTS is better:
create view post_with_answers AS
SELECT DISTINCT pq.postid
FROM qa_posts pq
WHERE EXISTS (SELECT 1
FROM qa_posts pa
WHERE pq.postid = pa.parentid AND
pa.type = 'A'
)
WHERE pq.type = 'Q';
$numQuery1 = "
SELECT d.*
, r.*
, AVG(c.on_time)+AVG(c.friendly)+AVG(c.language_skills)+AVG(c.professional) ranking
FROM comment c
LEFT
JOIN driver d
ON d.userid = c.driver_id
LEFT
JOIN driver_rental r
ON r.email = d.email
WHERE
(
$driver_rental IS NOT NULL
AND $driver_rental != ''
AND car_type_1 >= $_SESSION[car_type_1]
)
OR car_type_2>=$_SESSION[car_type_1]);
";
if the comment table have no driver_id record, the result is null, can I ignore comment table but in select statement cannot without it?
$numQuery1 = "SELECT driver.*, driver_rental.*, avg(comment.on_time)+avg(comment.friendly)+avg(comment.language_skills)+avg(comment.professional) as ranking
FROM comment
LEFT JOIN driver
ON
driver.userid = comment.driver_id
RIGHT JOIN driver_rental
ON
driver.email = driver_rental.email
WHERE
($driver_rental IS NOT NULL AND $driver_rental != '') AND (car_type_1>=$_SESSION[car_type_1] OR car_type_2>=$_SESSION[car_type_1])";
The idea is to not stress on driver table in either join. SO instead of second left join use right join with driver_rental
I am attempting to combine the contents of two separate (but very closely related) sql queries, but am finding it difficult to get something that doesn't return syntax errors.
The two queries that are currently working are
SELECT module.ModuleCode,
demonstrator.FK_Course,
slot.fk_ModuleCode,
programme.C_val,
Count(slotdemo.FK_Demonstrator) AS CountOfFK_Demonstrator
FROM programme
INNER JOIN (demonstrator
INNER JOIN (((module
INNER JOIN slot ON module.ModuleCode = slot.fk_ModuleCode))
INNER JOIN slotdemo ON slot.SlotNo = slotdemo.FK_SlotNo) ON demonstrator.StudentID = slotdemo.FK_Demonstrator) ON programme.COURSE = demonstrator.FK_Course
WHERE demonstrator.`undergraduate`=1
GROUP BY module.ModuleCode
and
SELECT week.Hour,
module.color,
module.moduleName,
module.num_ugdemos,
slot.fk_Room,
slot.fk_ModuleCode,
slottime.FK_Hour,
slottime.FK_SlotNo,
programme.C_val,
Count(slotdemo.FK_Demonstrator) AS CountOfFK_Demonstrator
FROM week
INNER JOIN ((programme
INNER JOIN ((module
INNER JOIN slot ON module.ModuleCode = slot.fk_ModuleCode)
INNER JOIN slottime ON slot.SlotNo = slottime.FK_SlotNo) ON programme.COURSE = module.FK_Course)
LEFT JOIN slotdemo ON slot.SlotNo = slotdemo.FK_SlotNo) ON week.Hour = slottime.FK_Hour
GROUP BY week.Hour,
module.ModuleCode,
module.moduleName,
module.color,
slot.SlotNo,
slot.fk_Room,
slottime.FK_Hour,
programme.C_val HAVING((programme.C_val)<9);
my preliminary attempt at combining the two is
SELECT
week.Hour,
module.color,
module.moduleName,
module.num_ugdemos,
slot.fk_Room,
slot.fk_ModuleCode,
slottime.FK_Hour,
slottime.FK_SlotNo,
programme.C_val,
Count(slotdemo.FK_Demonstrator) AS CountOfFK_Demonstrator
FROM
week
INNER JOIN
(
(
programme
INNER JOIN
(
demonstrator
INNER JOIN
(
(
(
module
INNER JOIN
slot
ON module.ModuleCode = slot.fk_ModuleCode
)
)
INNER JOIN
slotdemo
ON slot.SlotNo = slotdemo.FK_SlotNo
)
ON demonstrator.StudentID = slotdemo.FK_Demonstrator
)
ON programme.COURSE = demonstrator.FK_Course
)
ON week.Hour = slottime.FK_Hour
WHERE
demonstrator.`undergraduate`=1
GROUP BY week.Hour,
module.moduleName,
module.color,
slot.SlotNo,
slot.fk_ModuleCode,
slot.fk_Room,
slottime.FK_Hour,
programme.C_val
The error message, for what's it's worth (which is not much) is:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near 'ON week.Hour = slottime.FK_Hour WHERE
demonstrator.undergraduate=1 GROUP BY we' at line 17
I think you want to UNION Two datasets together. Just use the UNION operator:
(SELECT .... FROM t1)
UNION
(SELECT ... FROM t2)
Both selects should return the same columns, IF the names differ you can use AS to rename it.
Alright, let's to business ...
say I have a category (categoyid) '150 ', and would not bring anything that is in that category .....
It turns out that an article may be in multiple categories, and the more I block 150 in the category SELECT, it will still come to be linked to other categories ....
How do I, so that any item in the category '150 'is not sought in the SELECT even though he was also in another category than the '150' ...
Tables:
node
nodeid
contentid
url
publishdate
nodeinfo
nodeid
title
node_category
categoryid
nodeid
article
contentid
previewimage
=====================
I tried :
SELECT p.nodeid, p.contentid p.publishdate, p.url, c.categoryid, c.nodeid, a.previewimage, a.contentid, e.title FROM `node` AS p
INNER JOIN `nodecategory` AS c ON p.`nodeid` = c.`nodeid`
INNER JOIN `article` AS a ON p.`contentid` = a.`contentid`
INNER JOIN `nodeinfo` AS e ON p.`nodeid` = e.`nodeid`
WHERE c.`categoryid`
IN (73,74,77,105,71,70,72,76,100,80,79,78,81,108,145,146,82,142,83,97,153)
GROUP BY c.nodeid
ORDER BY p.`publishdate`
DESC LIMIT 4
I think you need a not-exists clause:
AND NOT EXISTS (
SELECT 1
FROM
`nodecategory` AS ic
WHERE
p.`nodeid` = ic.`nodeid`
AND ic.`categoryid` IN (150)
)
Here it is in your query, reformatted a bit:
SELECT
p.nodeid,
p.contentid,
p.publishdate,
p.url,
c.categoryid,
c.nodeid,
a.previewimage,
a.contentid,
e.title
FROM
`node` AS p
INNER JOIN `nodecategory` AS c ON p.`nodeid` = c.`nodeid`
INNER JOIN `article` AS a ON p.`contentid` = a.`contentid`
INNER JOIN `nodeinfo` AS e ON p.`nodeid` = e.`nodeid`
WHERE c.`categoryid`
IN (73,74,77,105,71,70,72,76,100,80,79,78,81,108,145,146,82,142,83,97,153)
AND NOT EXISTS (
SELECT 1
FROM
`nodecategory` AS ic
WHERE
p.`nodeid` = ic.`nodeid`
AND ic.`categoryid` IN (150)
)
GROUP BY c.nodeid
ORDER BY p.`publishdate`
DESC LIMIT 4
This should filter out nodes that are in one of the chosen categories, but are not also in those specified in the not-exists clause.