I have this query in sql, and i can't get it right.
I'm trying to make a query to get the results from chat.
select message, first_name, last_name, dataPost,picture
from chat_comms
right join utilizadores
on (chat_comms.id_writer = utilizadores.id_user)
or (chat_comms.id_reader = utilizadores.id_user)
where id_writer = 1
or (id_reader = 1 and id_writer = 13)
order by dataPost ASC
RESULT
the first_name, last_name, picture, should be different because they are different users, how can i solve this?
SQL FIDDLE:
VIEW SQL FIDDLE
It looks like you have the duplicate rows because one of the users is the "writer" and the other is the "reader". I'm guessing that you actually want one row for each row in chat_comms linked to both the "writer" and "reader". In this case you need to join to utilizadores twice instead of using the or:
select message, writer.first_name, writer.last_name, dataPost, picture
from chat_comms
left join utilizadores writer on chat_comms.id_writer = writer.id_user
left join utilizadores reader on chat_comms.id_reader = reader.id_user
where id_writer = 1
or (id_reader = 1 and id_writer = 13)
order by dataPost ASC
This is just a guess because you haven't adequately described the result you're trying to achieve.
please verify below mentioned query:
SELECT message, first_name, last_name, dataPost,picture
FROM chat_comms
RIGHT JOIN utilizadores
ON chat_comms.id_writer = utilizadores.id_user
OR (chat_comms.id_reader = utilizadores.id_user)
WHERE id_writer = 1
order by dataPost ASC
Hope that helps, thanks.
Related
I'm trying to write a simple query but the output is not correct:
Herer my code:
SELECT oc_order_product.order_id AS ordernumber, oc_order_product.quantity,
oc_order_product.model, oc_order_product.name,
oc_order.shipping_company, oc_order.shipping_firstname,
oc_order.shipping_lastname, oc_order.shipping_city, oc_product.location
FROM oc_order_product,
oc_order,
oc_product
WHERE oc_order.order_id = oc_order_product.order_id
AND oc_order.order_status_id = 1
AND oc_product.location = 1
ORDER BY ordernumber, oc_order_product.model
The output is a list of all products with the oc_order.order_status_id = 1 but the second AND (oc_product.location = 1) is not applied. What is wrong? I don't work with JOIN because I don't understand it really good.
Now using modern, explicit JOIN:
SELECT oc_order_product.order_id AS ordernumber, oc_order_product.quantity,
oc_order_product.model, oc_order_product.name,
oc_order.shipping_company, oc_order.shipping_firstname,
oc_order.shipping_lastname, oc_order.shipping_city, oc_product.location
FROM oc_order_product
INNER JOIN oc_order ON oc_order.order_id = oc_order_product.order_id
INNER JOIN oc_product ON oc_product.id = oc_order_product.product_id
WHERE oc_order.order_status_id = 1
AND oc_product.location = 1
ORDER BY ordernumber, oc_order_product.model
That last join
INNER JOIN oc_product ON oc_product.id = oc_order_product.product_id
is just a guess, since I don't know which columns to use!
jarlh is guessing that oc_product.id = oc_order_product.product_id.
I'm going to guess that your data element names are consistently and uniquely named throughout your schema and therefore suggest you use ultra-modern NATURAL JOIN:
SELECT order_id AS ordernumber, quantity,
model, name, shipping_company, shipping_firstname,
shipping_lastname, shipping_city, location
FROM oc_order_product
NATURAL JOIN oc_order
NATURAL JOIN oc_product
WHERE order_status_id = 1
AND location = 1
ORDER
BY ordernumber, model;
...not betting the farm on it, though.
This is the SQL statement:
select
ua.*, uuu.user_name
from
(select
ud.dev_id,
(select uud.user_id as user_id
from user_device uud
where ud.dev_id = uud.dev_id and assigned = 1) user_id,
(select count(1)
from user_device du
where du.dev_id = ud.dev_id) user_number,
de.license
from
user_device ud
inner join
device de on ud.dev_id = de.dev_id
where ud.user_id = 'XXXXXX') ua
left join
user_info uuu on uuu.user_id = ua.user_id
Execute the same SQL, it sometimes reports this error, but sometimes it runs just fine.
The error :
and this is what I want (with another user_id yesterday)
The error is pretty self-explanatory. I'm pretty sure it is referring to this subquery:
(select uud.user_id
from user_device uud
where ud.dev_id = uud.dev_id and assigned = 1
)
Clearly, this subquery is returning multiple rows under some circumstances. A quick and dirty fix is to add and rownum = 1 to the where clause.
You can determine where the duplicates are by running:
select uud.dev_id, count(*) as cnt
from user_device uud
where uud.assigned = 1
group by uud.dev_id
having count(*) > 1;
I'm working on a social project and there are some tables which will have similar records which i'd like to group them into one. but with my query in brings all the records instead of grouping them into one.
SELECT
users_pics.email
, pic = MIN(pic)
, count(pic) AS total
,fname
,lname
,profile_pix
,RTRIM(wardrobe) as wardrobe
,gender
, resp_email
FROM
users_pics
INNER JOIN
profile
ON
users_pics.email = profile.email, dbo.viva_friends
WHERE
users_pics.stat = 'new'
AND
resp_email = MMColParam2
OR
req_email= MMColParam
AND
Status = 'Yes '
GROUP BY
users_pics.email
,fname,lname
,profile_pix
,wardrobe
,gender
,req_email
,resp_email
ORDER BY
MAX(users_pics.u_pic_id) DESC
please is there a way out. when i hit on the run button i get this.
Looks like you just want a simple GROUP BY;
SELECT users_pics.email, MIN(pic) AS pic, COUNT(pic) AS total, MAX(fname) AS fname,
MAX(lname) AS lname, MAX(profile_pix) AS profile_pic,
RTRIM(MAX(wardrobe)) AS wardrobe, MAX(gender) AS gender, MAX(resp_email) resp_email
FROM users_pics
INNER JOIN profile
ON users_pics.email = profile.email,
dbo.viva_friends
WHERE (users_pics.stat = 'new' AND resp_email = MMColParam2)
OR (req_email= MMColParam AND Status = 'Yes ')
GROUP BY
users_pics.email
Since I can't see your table structure, I can't tell what dbo.viva_friends is used for. It looks like it may be possible to just remove.
I have the following query which gives gives req_no and order_no. order_no is a from a sub_query which you can see from below sql. For a few req_no there are more than one order_no's and because of that I get ORA-01427: single-row subquery returns more than one row.
I would like to display both the order_no for one req_no, how can I achieve this?
Any help is highly appreciable.
Thanks
P.S. Our client's one database is still Oracle 8i.
SELECT max_qst.req_no,
(SELECT DISTINCT max_odr.order_no
FROM maximo_orders max_odr,
maximo_order_revisions max_odv,
maximo_order_items max_odi,
maximo_order_dates max_odd,
maximo_requisition_order max_rqo,
maximo_requisition_details max_req
WHERE max_req.req_no = max_qst.req_no
AND max_req.req_yr = max_qst.req_yr
AND max_odr.order_no = max_odi.order_no
AND max_odi.order_item_id = max_odd.order_item_id
AND max_req.requisition_item_id = max_rqo.requisition_item_id
AND max_rqo.order_schedule_id = max_odd.order_schedule_id
AND max_odv.order_no = max_odi.order_no
AND max_odv.revision_no =
(SELECT MAX (max_alias.revision_no)
FROM maximo_order_revisions max_alias
WHERE max_alias.order_no = max_odv.order_no)
AND maximo_order_item (max_odi.order_no,
max_odv.revision_no,
max_odi.order_item_id
) = 'CONFIRMED'
)
FROM maximo_requisitions max_qst, maximo_requisition_details max_qsd
WHERE max_qst.qst_id = max_qsd.qst_id
AND max_qst.enter_date = '2001'
AND max_qst.req_no = 'PUR_12WX'
Update 1
Desired out put.
REQ_No ORDER_NO
PUR_12WX PR_9078
PUR_12WX PR_9079
Use a join instead of a correlated sub-query.
I've removed the max_qst references from the sub-query and moved them to the join predicate.
I've also just changed it to use a LEFT JOIN. This allows for the possibility of there being no order_no values returned.
SELECT
max_qst.req_no,
sub_query.order_no
FROM
maximo_requisitions max_qst
INNER JOIN
maximo_requisition_details max_qsd
ON max_qst.qst_id = max_qsd.qst_id
LEFT JOIN
(
SELECT DISTINCT
max_odr.order_no,
max_req.req_no,
max_req.req_yr
FROM
maximo_orders max_odr,
maximo_order_revisions max_odv,
maximo_order_items max_odi,
maximo_order_dates max_odd,
maximo_requisition_order max_rqo,
maximo_requisition_details max_req
WHERE
max_odr.order_no = max_odi.order_no
AND max_odi.order_item_id = max_odd.order_item_id
AND max_req.requisition_item_id = max_rqo.requisition_item_id
AND max_rqo.order_schedule_id = max_odd.order_schedule_id
AND max_odv.order_no = max_odi.order_no
AND max_odv.revision_no = (SELECT MAX (max_alias.revision_no)
FROM maximo_order_revisions max_alias
WHERE max_alias.order_no = max_odv.order_no)
AND maximo_order_item (max_odi.order_no, max_odv.revision_no, max_odi.order_item_id) = 'CONFIRMED'
)
suq_query
ON max_qst.req_no = sub_query.req_no
AND max_qst.req_yr = sub_query.req_yr
WHERE
max_qst.enter_date = '2001'
max_qst.req_no = 'PUR_12WX'
You can use
DISTINCT
or
WHERE ROWNUM = 1
But I'd suggest you investigate why you're getting more than one row returned: Have you misunderstood the data, have you missed a join, is there erroneous or duplicate data etc etc.
I am facing an issue while working with Zend Framework. Basically, I am trying to add an AND operator in a sub query using Left join. Please see the following query that my code is producing:
SELECT `d`.*, count(status) FROM `deal` AS `d` LEFT JOIN `payments` ON `payments`.deal_id = `d`.deal_id GROUP BY `d`.`deal_id` ORDER BY `created_date` desc
Code is:
$select = $this->_db->select()
->from(array('d'=>'deal'))
->joinLeftUsing('payments', 'deal_id', array('count(status)'))
->order("created_date $sortOrder")
->group("d.deal_id");
However, I want to add one more condition in my sub query i.e. Status = 1, please see the following output that I am willing to get.
SELECT `d`.*, count(status) FROM `deal` AS `d` LEFT JOIN `payments` ON `payments`.deal_id = `d`.deal_id AND status = 1 GROUP BY `d`.`deal_id` ORDER BY `created_date` desc
Let me know if someone have an idea about how I can achieve the same.
Thanks,
Gagz
I would recommend using joinLeft instead of joinLeftUsing
$select = $this->_db->select()
->from(array('d'=>'deal'))
->joinLeft('payments', 'payments.deal_id = d.deal_id and status = 1',
array('count(status)'))
->order("created_date $sortOrder")
->group("d.deal_id");
gives me
SELECT `d`.*, count(status) FROM `deal` AS `d`
LEFT JOIN `payments` ON payments.deal_id = d.deal_id and status = 1
GROUP BY `d`.`deal_id` ORDER BY `created_date ` ASC
We can use expression in Zend 3:
$join = new \Zend\Db\Sql\Expression('contractor_jobs.contractor_id = contractor_info.contractor_id AND
contractor_jobs.job_trade_id = '.$variableName.' ');
$select->join(
'contractor_jobs',
$join,
array('job_trade_id'),
$select::JOIN_LEFT
);
It works for me. You can also check as per your conditions. Thanks for asking this question.