SELECT *
FROM jobs
WHERE (SELECT DISTINCT jobs.*
FROM jobs, job_requests
WHERE (jobs.user_id = 1) OR
(job_requests.user_id = 1 AND job_requests.job_id = jobs.id)
)
This sql gives me:
Mysql::Error: Operand should contain 1 column(s).
If I execute the select from the where clause it works
SELECT DISTINCT jobs.* FROM jobs, job_requests
WHERE (jobs.user_id = 1) OR
(job_requests.user_id = 1 AND job_requests.job_id = jobs.id)
Could somebody explain me why? This query is generated by rails activerecord so the main select is needed.
The ror code:
has_many :my_jobs, :class_name=>"Job", :finder_sql =>
'SELECT DISTINCT jobs.* ' +
'FROM jobs, job_requests ' +
'WHERE (jobs.user_id = #{id}) OR ' +
'(job_requests.user_id = #{id} AND job_requests.job_id = jobs.id AND job_requests.request_status IN ("requested", "confirmed") )'
I am submitting my answer based on the additional information provided. The query below should address your requirement.
Job.all(:conditions=> [ "
jobs.user_id = ? OR
EXISTS (
SELECT *
FROM job_requests AS B
WHERE B.job_id = jobs.id AND B.user_id = ?
)", user_id, user_id ]
)
If you want an efficient version of the same query then you should go with UNIONs.
sql = "
SELECT *
FROM jobs A WHERE A.user_id = ?
UNION
SELECT *
FROM jobs A, job_requests B
WHERE A.id = B.job_id AND B.user_id = ?
"
Job.find_by_sql(Job.send(:sanitize_sql_array, [sql, user_id, user_id]))
The first query can be converted to a named_scope.
class Job < ActiveRecord::Base
named_scope :for_user, lambda { |user_id| { :conditions=> [ "
jobs.user_id = ? OR
EXISTS (
SELECT *
FROM job_requests AS B
WHERE B.job_id = jobs.id AND B.user_id = ?
)", user_id, user_id ] }
}
end
Now you can use the named_scope as follows:
Jobs.for_user(current_user)
SQL query: Documentation
SELECT (
'95.86.122.224', NOW( ) , '95.86.122.224', '95.86.122.224'
)
FROM orders
LEFT JOIN visitors ON visitors.ip = '95.86.122.224'
WHERE visitors.ip IS NULL
LIMIT 0 , 30
MySQL said: Documentation
#1241 - Operand should contain 1 column(s)
If I have understood you correctly you need to restructure as
SELECT * FROM jobs
WHERE EXISTS
(SELECT DISTINCT jobs.*
FROM jobs, job_requests
WHERE (jobs.user_id = 1)
OR (job_requests.user_id = 1 AND job_requests.job_id = jobs.id))
I'm guessing you only want to select from jobs where the inner query is true, the above will do this for you.
Following code will work, if you want to get the jobs with job requests for a given user:
Job.all( :joins => :jobs_requests,
:conditions => ["job_requests.user_id = ?", user_id])
Related
I have this sql script that selects duplicate record . i want to convert it to an update statement where it updates are particular column called STATUS in the table with the string "Duplicated"
Dbms is Oracle
Select * From (
select * from VisaNnsMainTable
Where TRN_TYPE = '500' OR TRN_TYPE = '2500') A
inner join (
select ARN,WAN_NUM, ABS(TRN_AMOUNT) AS AMOUNT
from VisaNnsMainTable
Where TRN_TYPE = '500' OR TRN_TYPE = '2500'
group by ARN, WAN_NUM,ABS(TRN_AMOUNT)
having count(*) > 1
) B on A.ARN = B.ARN
and A.WAN_NUM = B.WAN_NUM
and ABS(A.TRN_AMOUNT) = B.AMOUNT
ORDER BY A.WAN_NUM
Please help me review this I think it does what I want ,but I am not sure if i wrote it well
UPDATE VisaNnsMainTable
SET EXCEPTION = 'Duplicate'
where CONCAT(ARN,WAN_NUM,ABS(TRN_AMOUNT)) in
( select CONCAT(ARN,WAN_NUM,ABS(TRN_AMOUNT))
from VisaNnsMainTable
group by CONCAT(ARN,WAN_NUM,ABS(TRN_AMOUNT))
having count(*) > 1 ) AND (TRN_TYPE = '500' OR TRN_TYPE = '2500')
I am in a need of running a complex SQL query and because I didn't know how to construct the query using ActiveRecod, I had to use a raw SQL query by using find_by_sql:
scope :get_cars, -> do
find_by_sql('select * from
(SELECT cars.*,
manufacturer.company_name AS manufacturer_company_name,
services.a_num AS service_a_num,
(SELECT car_documents.file_url FROM car_documents
WHERE car_documents.car_id = cars.id AND car_documents.doc_type = 1 LIMIT 1) AS doc1_file_url,
(SELECT car_documents.file_s3_url FROM car_documents
WHERE car_documents.cart_id = cars.id AND car_documents.doc_type = 3 LIMIT 1) AS file_inv,
(SELECT car_data.demand_lvl FROM car_data
WHERE car_data.car_id = cars.id) AS demand_lvl,
(SELECT car_logs.invoice FROM car_logs
WHERE car_logs.car_id = cars.id AND car_logs.invoice = 1 LIMIT 1) AS invoice_sent
FROM "cars"
INNER JOIN "services" ON "services"."id" = "cars"."service_id"
LEFT JOIN manufacturers ON manufacturers.id = cars.manufacturer_id
WHERE (cars.status_id != 6
AND cars.delivery_date < NOW() - INTERVAL \'15 days\'
) ORDER BY cars.pickup_date ASC) t
Where doc1_file_url IS NULL OR file_inv IS NULL OR invoice_sent IS NULL')
end
The output is an array. How do I convert this array into an ActiveRecord object? Or possibly, is there any workaround?
I think, You can use the from() method from the Active Record interface. You can use a subquery as a table for a SQL select statement with help of from() method. check the below example. In this way, you will get the active record object.
subquery = Setting.limit(10)
Setting.from("(#{subquery.to_sql}) settings")
In your case,
subquery = "(SELECT cars.*,
manufacturer.company_name AS manufacturer_company_name,
services.a_num AS service_a_num,
(SELECT car_documents.file_url FROM car_documents
WHERE car_documents.car_id = cars.id AND car_documents.doc_type = 1 LIMIT 1) AS doc1_file_url,
(SELECT car_documents.file_s3_url FROM car_documents
WHERE car_documents.cart_id = cars.id AND car_documents.doc_type = 3 LIMIT 1) AS file_inv,
(SELECT car_data.demand_lvl FROM car_data
WHERE car_data.car_id = cars.id) AS demand_lvl,
(SELECT car_logs.invoice FROM car_logs
WHERE car_logs.car_id = cars.id AND car_logs.invoice = 1 LIMIT 1) AS invoice_sent
FROM "cars"
INNER JOIN "services" ON "services"."id" = "cars"."service_id"
LEFT JOIN manufacturers ON manufacturers.id = cars.manufacturer_id
WHERE (cars.status_id != 6
AND cars.delivery_date < NOW() - INTERVAL \'15 days\'
) ORDER BY cars.pickup_date ASC)"
I have a SQL query I want to convert it to a dql query in symfony 3.4,
this is the SQL query:
SELECT c.*
FROM chat c INNER JOIN (
SELECT idsender, MAX(created) created
FROM chat
WHERE idreceiver = 2 OR idsender = 2
GROUP BY idsender
) t ON t.idsender = c.idsender AND t.created = c.created
WHERE c.idreceiver = 2
This is my attempt:
public function findAllChatBox($idreceiver,$idsender)
{
return $this->getEntityManager()
->createQuery(
'SELECT c FROM offer Bundle:chat c WHERE c.idreceiver = :idreceiver AND idsender=:idsender'
)
->groupBy('c.idsender')
->join('t ON t.idsender = c.idsender AND t.created = c.created
WHERE c.idreceiver =: idreceiver')
->setParameter('idreceiver',$idreceiver)
->setParameter('idsender',$idsender)
->getResult();
}
But I had a syntax error, any suggestions please?
syntax error here 'join('t ON t.idsender = c.idsender AND t.created = c.created WHERE c.idreceiver =: idreceiver')'
I have the following query
SELECT
dtc.coupon_type_company_name,
COUNT(*) * dtc.coupon_type_company_coupon_amount AS 'Total_Coupon_To_Be_Used',
dtc.coupon_type_company_coupon_months_combinable
FROM
[dbo].[coupon_type_Company_User] dtcu
JOIN
coupon_type_Company dtc ON dtcu.coupon_type_Company_ID = dtc.id
JOIN
person p ON dtcu.userID = p.userID
WHERE
coupon_type_company_coupon_is_combinable = 1
OR coupon_type_company_has_coupon = 1
AND dtc.companyID = 1081
AND p.is_active = 1
GROUP BY
dtc.coupon_type_company_name,dtc.coupon_type_company_coupon_amount,
dtc.coupon_type_company_coupon_months_combinable
This returns the following:
What I want to have however is just one column and one row that should take the SUM of my middle column (count(*)*dtc.coupon_type_company_coupon_amount).
How could I achieve this and prevent doing this in my code backend (C#)
You can wrap your query like this:
SELECT SUM(Total_Coupon_To_Be_Used) AS the_sum
FROM (
your query
) s
Use a "Table Expression", as in:
select sum(Total_Coupon_To_Be_Used) from (
SELECT dtc.coupon_type_company_name,
count(*) * dtc.coupon_type_company_coupon_amount as 'Total_Coupon_To_Be_Used',
dtc.coupon_type_company_coupon_months_combinable
FROM [dbo].[coupon_type_Company_User] dtcu
JOIN coupon_type_Company dtc ON dtcu.coupon_type_Company_ID = dtc.id
JOIN person p ON dtcu.userID = p.userID
WHERE coupon_type_company_coupon_is_combinable = 1
or coupon_type_company_has_coupon = 1
and dtc.companyID = 1081
AND p.is_active = 1
GROUP BY
dtc.coupon_type_company_name,dtc.coupon_type_company_coupon_amount,
dtc.coupon_type_company_coupon_months_combinable
) x
I have two tables EXERCISE and EXERCISEUSER. I need to list all exercise entries and put an additional field in the query, which will return if that exercise exists in the table EXERCISEUSER. In other words, I need know if the user did that exercise. If so, it will have a row in EXERCISEUSER.
My current query is:
SELECT
"E".*,
"T"."NAME" AS "LEVEL"
FROM
"EXERCISE" AS "E"
INNER JOIN
"EXERCISETYPE" AS "T"
ON
E.STO_FK_EXERCISETYPEEXERCISE = T.PK_EXERCISETYPE
INNER JOIN
"LEVEL" AS "L"
ON
L.PK_LEVEL = E.STO_FK_LEVELEXERCISE
WHERE
(
E.STATUS = 1)
AND (
L.STATUS = 1)
AND (
L.PK_LEVEL = 5)
ORDER BY
"T"."ORDER" ASC
I will provide PK_USER too.
Thanks!
Well, i use a subquery, and reach the result i want.
SELECT
"E".*,
"T"."NAME" AS "LEVEL",
( SELECT COUNT(*) FROM STOUSER.EXERCISEUSER AS EU WHERE EU.STO_FK_EXERCISEEXERCISEUSER = E.PK_EXERCISE AND EU.STO_FK_USEREXERCISEUSER = 5978 ) AS MAKE_EXER_NUM
FROM
"STOUSER"."EXERCISE" AS "E"
INNER JOIN
"STOUSER"."EXERCISETYPE" AS "T"
ON
E.STO_FK_EXERCISETYPEEXERCISE = T.PK_EXERCISETYPE
INNER JOIN
"STOUSER"."LEVEL" AS "L"
ON
L.PK_LEVEL = E.STO_FK_LEVELEXERCISE
WHERE
(
E.STATUS = 1)
AND (
L.STATUS = 1)
AND (
L.PK_LEVEL = 5)
ORDER BY
"T"."ORDER" ASC
Thanks!
I think this should be done with a LEFT OUTER JOIN.