Query inside NOT IN statement Doctrine - mysql

I found this sentence in the code:
$dql
= <<<DQL
SELECT u FROM AppBundle:User u
JOIN u.Roles r
JOIN u.team t
WHERE u.id NOT IN (
SELECT user.id
FROM GameBundle:Goal g
JOIN g.user user
WHERE
g.objective = :objective
)
AND
r.profile = :sales_profile
AND
r.company = :company
AND
u.onlyStatus NOT IN (:status)
DQL;
I don't know how to works that query inside NOT IN sentence, help me please.
I need to know:
What return the query inside of NOT IN, (data types, so on...)
How to works a query inside of NOT IN (is posible ?)

SELECT u FROM AppBundle:User u
JOIN u.Roles r
JOIN u.team t
WHERE u.id NOT IN (
SELECT user.id
FROM GameBundle:Goal g
JOIN g.user user
WHERE
g.objective = :objective
)
this means take all the users that don't currently have 'objective' in the 'Goal' table.
Is this what you needed ?

Related

Display a record although the condition return zero record

I have MySql query like this:
SELECT name, id_number, hr.id, file, created_at, updated_at
From users u
LEFT JOIN homework_targets ht on u.class_id = ht.class_id
LEFT JOIN homework_replies hr on u.id = hr.user_id
WHERE u.role = 'student' AND hr.homework_id = 8
This query work just fine, but not like what I expected. I want to display all the student (users) record is displayed although they don't have a record on homework_replies that match the homework_targets is it possible? if it possible how can I accomplish it? thanks.
Move the hr.homework_id condition from WHERE to ON to get true LEFT JOIN result:
SELECT name, id_number, hr.id, file, created_at, updated_at
From users u
LEFT JOIN homework_targets ht on u.class_id = ht.class_id
LEFT JOIN homework_replies hr on u.id = hr.user_id AND hr.homework_id = 8
WHERE u.role = 'student'
(If you have it in the WHERE clause, you'll get regular INNER JOIN result.)

MySQL Where Statement While checking value

$sql = "SELECT cc.name AS c_name, ev.name AS event_name, ev.eventModeId AS event_mode, ev.carClassHash, cc.carClassHash
FROM EVENT_DATA e
INNER JOIN PERSONA p ON e.personaId = p.ID
INNER JOIN CUSTOMCAR cc ON cc.ownedCarId = e.carId
INNER JOIN CAR_CLASSES ccs ON ccs.store_name = cc.name
INNER JOIN USER u ON u.ID = p.USERID
LEFT JOIN BAN b ON b.user_id = u.ID
INNER JOIN EVENT ev ON ev.ID = e.EVENTID
WHERE (p.name = ?
AND ev.carClassHash = cc.carClassHash)";
This query works for me except I'd also like to display any carClassHash with the value '607077938'. Is there a way I could somehow keep the above query to check if the event hash matches the car class hash but also still display values where the carClassHash (cc.carClassHash) is equal to '607077938'?
Thanks! :)
Why not use OR in the WHERE clause?
[...]
WHERE ((p.name = ? AND ev.carClassHash = cc.carClassHash) OR (cc.carClassHash = 607077938))
This should work if that value is an integer. If it's a string, use quotes around it.

How to get monthly postcount of phpBB users with SQL query?

I'm new to this SQL stuff, working with phpmyadmin. I already managed to get all usernames but don't know how to get postcount and especially postcounts within a defined periode of time (month). This is what i got so far:
SELECT DISTINCT
ug.`user_id`,
u.`username`,
pfd.`pf_full_name`
FROM
`phpbb3_user_group` AS ug
INNER JOIN
`phpbb3_groups` AS g ON g.group_name = 'User'
INNER JOIN
`phpbb3_users` AS u ON u.`user_id` = ug.`user_id`
INNER JOIN
`phpbb3_profile_fields_data` AS pfd ON pfd.`user_id` = ug.`user_id`
WHERE
ug.`group_id` = g.`group_id` AND u.`user_type` = 0
ORDER BY
ug.`user_id`
SELECT user_id FROM phpbb_users u WHERE u.user_type IN (0, 3, 1) AND u.user_regdate > 1648771200 AND (u.user_lastvisit > 0 AND u.user_lastvisit < 1651276800) ORDER BY u.user_regdate ASC
April add user sql

Convert SQL query to Rails ActiveRecord query

I have an sql query with multiple left joins that works fine:
query = <<-eos
select date(t.completed_at) completed_date, s.id district, assignee_id, u.first_name, u.last_name, count(t.id) completed_tasks
from tasks t
left join tickets k on k.id = t.ticket_id
left join installations i on i.id = k.installation_id
left join administrative_areas a on i.ward_id = a.id
left join service_areas s on s.id = a.service_district_id
left join users u on u.id = t.assignee_id
where 1 = 1
and s.id = '#{district_id}'
and t.status = '#{status}'
and t.kind = 1
and t.completed_at >= '#{days_ago.days.ago.beginning_of_day.to_s(:db)}'
and t.completed_at <= '#{days_until.days.ago.beginning_of_day.to_s(:db)}'
group by date(t.completed_at), s.id, s.name, u.first_name, u.last_name, t.assignee_id
eos
I got this value after mapping: [{:completed_date=>"2015-07-11", :district=>"1339", :assignee_id=>"215371", :assignee_name=>nil, :first_name=>"John_9", :last_name=>"Ant", :completed_tasks=>"1"}] for the sql query.
But I want to stop using the sql query and switch to ActiveRecord query and I have it converted to ActiveRecord like this:
Task.joins("LEFT JOIN tickets k ON k.id = tasks.ticket_id").
joins("LEFT JOIN installations i ON i.id = k.installation_id").
joins("LEFT JOIN administrative_areas a ON i.ward_id = a.id").
joins("LEFT JOIN service_areas s ON s.id = a.service_district_id").
joins("LEFT JOIN users u ON u.id = tasks.assignee_id").
where(["s.id = ? and tasks.status = ? and tasks.kind = ? and tasks.completed_at >= ? and tasks.completed_at <= ?", 26, "#{status}", 1, "#{days_ago.days.ago.beginning_of_day.to_s(:db)}", "#{days_until.days.ago.beginning_of_day.to_s(:db)}"]).
select('date(tasks.completed_at) as completed_date, s.id as district, assignee_id, u.first_name, u.last_name, count(tasks.id) as completed_tasks').
group("date(tasks.completed_at), s.id, s.name, u.first_name, u.last_name, tasks.assignee_id")
But the problem I have here is trying to do a select from multiple columns in different tables, the only value that the ActiveRecord query returns belong to the task table alone. I don't know what am doing wrong, maybe it's the left joins or the select
[#<Task status: 1, assignee_id: 215356, kind: 1>]
Please, how do I convert the above sql query to ActiveRecord query and get the same result?
You can use Scuttle.io the first result is always terrible, but try to configure associations in second tab. And please try to avoid constructions like this:
where("params.id = #{param[:id]}")
it is unsecured (sql injection)!

Mysql multiple tables left join last table

I have checked several threads on here and cannot find the answer to my question....
I am trying to do a mysql query with multiple tables and left join the last table. It does not like the left join and gives me no results when I include it. Any help is greatly appreciated :)
(I am using Joomla)
query = "SELECT DISTINCT u.id as uid, CONCAT(u.first_name,' ',u.last_name) as name1,
u.grad as grad, u.opt_out as opt_out
FROM #__bl_teams as t, #__bl_regions as r, #__users as u
LEFT JOIN #__bl_paid as pd ON pd.u_id = u.id
WHERE u.team_id = t.id AND u.team_id != '' AND u.s_id = $sid
AND ((t.id = $tid)OR($tid=0)) AND (t.id IN ($teamsfull))
AND ( (t.id IN(".$tc_teams."))OR(".$tc_id." = 0))
AND ((r.id = ".$mid.")OR(".$mid." = 0))
AND ((r.s_id = ".$sid.")OR(".$mid." = 0))
AND ( (FIND_IN_SET(t.id,r.teams) )OR(".$mid." = 0) )
AND u.id NOT IN($paidrows) AND u.id NOT IN ($rsrows) GROUP BY u.id";
$db->setQuery($query, $pageNav->limitstart, $pageNav->limit);
$rows50 = $db->loadObjectList();
You are mixing old-style joins and new style joins. That is your problem. The table aliases aren't recognized throughout the from statement, the way you expect them to be. Buried in the documentation is this warning:
However, the precedence of the comma operator is less than of INNER
JOIN, CROSS JOIN, LEFT JOIN, and so on. If you mix comma joins with
the other join types when there is a join condition, an error of the
form Unknown column 'col_name' in 'on clause' may occur. Information
about dealing with this problem is given later in this section.
Simple rule: Never use commas in the from clause.
So, rewrite your query using explicit joins and that will fix the problem.
I don't know the ctual tables, but this query is too long and will have unexpected results results. Anyways, Left join the other tables too.
query = "SELECT DISTINCT u.id as uid, CONCAT(u.first_name,' ',u.last_name) as name1,
u.grad as grad, u.opt_out as opt_out
FROM #__users as u
LEFT JOIN #__bl_paid as pd ON pd.u_id = u.id
LEFT JOIN #__bl_teams as t ON t.u_id = u.id
LEFT JOIN #__bl_regions as r ON r.u_id = u.id
WHERE u.team_id != ''
AND u.s_id = $sid
AND ((t.id = $tid)
OR($tid=0))
AND (t.id IN ($teamsfull))
AND ( (t.id IN(".$tc_teams."))
OR(".$tc_id." = 0))
AND ((r.id = ".$mid.")
OR(".$mid." = 0))
AND ((r.s_id = ".$sid.")
OR(".$mid." = 0))
AND ( (FIND_IN_SET(t.id,r.teams) )
OR(".$mid." = 0) )
AND u.id NOT IN($paidrows)
AND u.id NOT IN ($rsrows)
GROUP BY u.id";