I want to get all fields from one table and use DISTINCT with the second table.
I have this:
SELECT stats.*,
DISTINCT(visit_log.blog_id) AS bid
FROM stats
INNER JOIN visit_log ON stats.blog_id = visit_log.blog_id
But I get this error:
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 'DISTINCT(visit_log.blog_id) AS bid FROM stats INNER JOIN visit_log ON stats.blog' at line 1
Any idea?
Instead of joining against visit_log, you can construct a derived table containing only the distinct blog_id values.
select stats.*, v.blog_id
from stats
inner join ( select distinct blog_id from visit_log where stats.blog_id = visit_log.blog_id ) as v
SELECT stats.*, dr.blog_id
FROM stats
INNER JOIN (SELECT DISTINCT(visit_log.blog_id) AS bid FROM visit_log) AS dr
ON stats.blog_id = dr.blog_id
You are only selecting blog_id from visit_log which is the column you are joining on. So your query is much like:
select *
from stats s
where
exists (select null from visit_log v where s.blog_id = v.blog_id)
select * from visit_log v where v.blog_id in/= (select s.blog_id from stats s)
Related
I have 3 tables which are interconnected and i want to select columns from two tables and counts from table 3. If anyone is aware on this, any hint would be appreciated.
Below is the sql i tried, but the count is getting repeated
SELECT distinct p.p_id, p.p_f6, p.p_l4,m.m_id, (
SELECT COUNT(*)
FROM ttokens t where t.pdetail_id = p.pdetail_id
) AS token_count
FROM tparking p,ttokens t LEFT join ttokens_md m ON t.trefn_id = m.trefn_id
WHERE t.pdetail_id = p.pdetail_id
You can try to use JOIN with subquery to get your count instead of selcet subquery.
SELECT p.p_id, p.p_f6, p.p_l4,m.m_id,t.cnt
FROM tparking p
JOIN (
SELECT pdetail_id,COUNT(*) cnt
FROM ttokens
GROUP BY pdetail_id
) t ON t.pdetail_id = p.pdetail_id
LEFT join ttokens_md m ON t.trefn_id = m.trefn_id
Note
I would use JOIN instead of , comma with where condition to connect two tables,, is an old style.
I need to do a correlated SQL Query and for that purpose i need to provide an alias to outer query which in which I perform an inner join. I am not able to do the alias
SELECT DISTINCT(name)
FROM PERSON
INNER JOIN M_DIRECTOR AS dira
ON (dira.PID = M_DIRECTOR.PID) as dira
WHERE 9 > (
SELECT COUNT(MID) FROM M_DIRECTOR WHERE name = dira.name
) ;
I didn't really understand what you want to do, but I guess
select
distinct p.name,
count(d.MID) cnt
from
hindi2_PERSON p
inner join
hindi2_M_DIRECTOR d
on
p.PID = d.PID
group by
p.name
having count(d.MID) > 9
;
would do what you want
I dont know what you are asking and what you mean by make an alias to an eniter result ?
but you are doing
select distinct(name) as othername
which is you are selecting name and you are giving here othername as an alias
then you retrieve it in result
$row['othername']
There's still something missing. From what you write, there is a field name in the M_DIRECTOR table?
Please show all the tables and attributes involved, use an SQL Fiddle to prepare an example.
SELECT DISTINCT(name)
FROM PERSON as p
INNER JOIN (
SELECT COUNT(MID), PID FROM M_DIRECTOR WHERE name = dira.name
) as d
ON (p.PID = d.PID) ;
I try to select the the rows with the newest timestamp in change_date from a table in a LEFT JOIN. I really don't know why this query fails:
SELECT
i.ID, i.title, i.create_date,
u1.username creator_name,
u2.username assignee
FROM item i
LEFT JOIN user u1 ON u1.login_IDFK = i.creator_IDFK
LEFT JOIN user u2 ON u2.login_IDFK = i.assigned_to_IDFK
LEFT JOIN (
SELECT MAX(change_date), item_IDFK FROM item_state GROUP BY item_IDFK
) AS ist ON ist.item_IDFK = i.ID
I get the following error
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 ') AS ist ON ist.item_IDFK = i.ID' at line 2 (Code: 1064)
Query works great without the last LEFT JOIN
(SELECT change_date, item_IDFK FROM item_state GROUP BY item_IDFK)
You are using a group by clause without an aggregate. Each item in the select list must either be represented in the group by clause, or be part of an aggregate expression
I.E.
(Select Max(Change_Date), item_IDFK from item_state group by item_IDFK)
try to save your last subquery in a view table, and after that, left join from that table, and see if the syntax error persists.
I have the following SQL statement
SELECT be.*, it.order_number
FROM songs AS be
INNER JOIN
(
SELECT song_id, order_number
FROM items
WHERE order_status = 1
) it
ON be.id = it.song_id
INNER JOIN orders AS or
ON it.order_number = or.order_number
WHERE be.active = 0
I can't seem to understand why this statement does not produce any results. When I remove the following line;
INNER JOIN orders AS or
ON it.order_number = or.order_number
It seems to produce results, and I know that the order_number does exist in the orders table - so it's clearly incorrect syntax but i'm not sure where to go from here? Appreciate the help.
The problem in this particular instance is that the or in the query is a reserved word. You can use that if you wish, but you'll have to quote it, like so
SELECT be.*, it.order_number
FROM songs AS be
INNER JOIN
(
SELECT song_id, order_number
FROM items
WHERE order_status = 1
) it
ON be.id = it.song_id
INNER JOIN orders AS "or"
ON it.order_number = "or".order_number
WHERE be.active = 0
Generally though, for readability, I'd avoid such names. If you have to quote it or escape it, it's probably a bad name.
Can anyone see what is wrong with the below query?
When I run it I get:
#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 where a.CompetitionID = Competition.CompetitionID' at line 8
Update Competition
Set Competition.NumberOfTeams =
(
SELECT count(*) as NumberOfTeams
FROM PicksPoints
where UserCompetitionID is not NULL
group by CompetitionID
) a
where a.CompetitionID = Competition.CompetitionID
The main issue is that the inner query cannot be related to your where clause on the outer update statement, because the where filter applies first to the table being updated before the inner subquery even executes. The typical way to handle a situation like this is a multi-table update.
Update
Competition as C
inner join (
select CompetitionId, count(*) as NumberOfTeams
from PicksPoints as p
where UserCompetitionID is not NULL
group by CompetitionID
) as A on C.CompetitionID = A.CompetitionID
set C.NumberOfTeams = A.NumberOfTeams
Demo: http://www.sqlfiddle.com/#!2/a74f3/1
Thanks, I didn't have the idea of an UPDATE with INNER JOIN.
In the original query, the mistake was to name the subquery, which must return a value and can't therefore be aliased.
UPDATE Competition
SET Competition.NumberOfTeams =
(SELECT count(*) -- no column alias
FROM PicksPoints
WHERE UserCompetitionID is not NULL
-- put the join condition INSIDE the subquery :
AND CompetitionID = Competition.CompetitionID
group by CompetitionID
) -- no table alias
should do the trick for every record of Competition.
To be noticed :
The effect is NOT EXACTLY the same as the query proposed by mellamokb, which won't update Competition records with no corresponding PickPoints.
Since SELECT id, COUNT(*) GROUP BY id will only count for existing values of ids,
whereas a SELECT COUNT(*) will always return a value, being 0 if no records are selected.
This may, or may not, be a problem for you.
0-aware version of mellamokb query would be :
Update Competition as C
LEFT join (
select CompetitionId, count(*) as NumberOfTeams
from PicksPoints as p
where UserCompetitionID is not NULL
group by CompetitionID
) as A on C.CompetitionID = A.CompetitionID
set C.NumberOfTeams = IFNULL(A.NumberOfTeams, 0)
In other words, if no corresponding PickPoints are found, set Competition.NumberOfTeams to zero.
For the impatient:
UPDATE target AS t
INNER JOIN (
SELECT s.id, COUNT(*) AS count
FROM source_grouped AS s
-- WHERE s.custom_condition IS (true)
GROUP BY s.id
) AS aggregate ON aggregate.id = t.id
SET t.count = aggregate.count
That's #mellamokb's answer, as above, reduced to the max.
You can check your eav_attributes table to find the relevant attribute IDs for each image role, such as;
Then you can use those to set whichever role to any other role for all products like so;
UPDATE catalog_product_entity_varchar AS `v` INNER JOIN (SELECT `value`,`entity_id` FROM `catalog_product_entity_varchar` WHERE `attribute_id`=86) AS `j` ON `j`.`entity_id`=`v`.entity_id SET `v`.`value`=j.`value` WHERE `v`.attribute_id = 85 AND `v`.`entity_id`=`j`.`entity_id`
The above will set all your 'base' roles to the 'small' image of the same product.