I have the following query
SELECT a.id, a.fecha, a.ser, a.numero, c.nombre_apellido, a.estado, a.tipo, a.articulo, a.precio_asignado, a.retirada, a.pronta, a.precio, a.confirmada, d.marca, a.modelo, a.fecha_prometido, a.fecha_asignado, a.presupuesto, a.cant_llamados
FROM (
(
(
ordenes_servicio_bitacora b
LEFT JOIN ordenes_reparaciones a ON b.id_orden = a.id
)
LEFT JOIN clientes c ON a.cliente_id = c.id
)
LEFT JOIN marcas d ON a.marca_id = d.id
)
ORDER BY b.id_bitacora DESC
LIMIT 10
and it brings me this
as you see the id column of the order is repeated because of the cross with bitacora table i need not to be repeated, any ideas? Thanks in advance.
DISTINCT should do the job in your case, as all the columns' data are repeated for the row not just the id column:
SELECT DISTINCT
a.id, a.fecha, a.ser, a.numero,
c.nombre_apellido, a.estado,
a.tipo, a.articulo, a.precio_asignado,
a.retirada, a.pronta, a.precio,
a.confirmada, d.marca, a.modelo,
a.fecha_prometido, a.fecha_asignado,
a.presupuesto, a.cant_llamados
FROM ordenes_servicio_bitacora b
LEFT JOIN ordenes_reparaciones a ON b.id_orden = a.id
LEFT JOIN clientes c ON a.cliente_id = c.id
LEFT JOIN marcas d ON a.marca_id = d.id
ORDER BY b.id_bitacora DESC
LIMIT 10
SELECT a.id, a.fecha, a.ser, a.numero, c.nombre_apellido, a.estado, a.tipo, a.articulo, a.precio_asignado, a.retirada, a.pronta, a.precio, a.confirmada, d.marca, a.modelo, a.fecha_prometido, a.fecha_asignado, a.presupuesto, a.cant_llamados
FROM (
(
(
(SELECT DISTINCT id_orden
FROM `ordenes_servicio_bitacora`
ORDER BY id_bitacora DESC
LIMIT 10) b
LEFT JOIN ordenes_reparaciones a ON b.id_orden = a.id
)
LEFT JOIN clientes c ON a.cliente_id = c.id
)
LEFT JOIN marcas d ON a.marca_id = d.id
)
i just made a subquery and the job is done, thanks by the answers and comments :)
Related
I have 3 column here in tblItem
purchase
stock
calculate
and this 3 column have to join with tblMeasurement
name
I have tried this, but when I display 3 columns show the same all.
SQL
$sql = "SELECT A.itemID, A.categoryID, A.purchaseMeasurementID, A.stockMeasurementID, A.calculationMeasurementID, A.itemName, B.itemCategoryName, C.measurementName
FROM tblPurItem A
LEFT JOIN tblPurItemCategory B
ON A.categoryID = B.itemcategoryID
LEFT JOIN tblPurMeasurement C
ON A.purchaseMeasurementID= C.measurementID";
As you see my sql above, it has join only for purchaseMeasurementID. How do I join stockMeasurementID and calculationMeasurementID based on also tblPurMeasurement ? Is there any missing here?
Try using this subquery instead.
SELECT A.itemID, A.categoryID, A.purchaseMeasurementID, A.stockMeasurementID, A.calculationMeasurementID, A.itemName
, (select B.itemCategoryName from tblPurItemCategory B where A.categoryID = B.itemcategoryID limit 1) as itemCategoryName
, (select C.measurementName from tblPurMeasurement C where A.purchaseMeasurementID= C.measurementID limit 1) as purchasemeasurementName
, (select C.measurementName from tblPurMeasurement C where A.stockMeasurementID= C.measurementID limit 1) as stockmeasurementName
, (select C.measurementName from tblPurMeasurement C where A.calculationMeasurementID= C.measurementID limit 1) as calculationmeasurementName
FROM tblPurItem A
I want to get the last item as result using max(), but i'm just getting the first item even if im using max()
Here's the SQL code:
SELECT r.correct, r.items, r.percentage,MAX(r.date_taken) as date_taken,
u.username,u.FN, u.user_course_type,
IFNULL(u.user_major_type,'N/A') as user_major_type,u.level_name,
u.section_name
FROM bcc_fs_exam_result r
INNER JOIN
(SELECT u.id_user, u.username, CONCAT(u.lastname,', ',u.firstname) as FN,
c.user_course_type, m.user_major_type, l.level_name, s.section_name
FROM bcc_fs_user u
LEFT JOIN bcc_fs_user_course c on c.id_user_course = u.id_user_course
LEFT JOIN bcc_fs_user_major m on m.id_user_major = u.id_user_major
LEFT JOIN bcc_fs_group_level l ON l.id_level = u.id_level
LEFT JOIN bcc_fs_group_section s ON s.id_section = u.id_section
) u ON r.id_user = u.id_user WHERE r.id_exam = 5 GROUP BY r.id_user
TIA
What I take from your question is that you want to get the "correct", "items", "percentage" ect. columns of the row in bcc_fs_exam_result which has the last or first date.
If that's correct then you can filter bcc_fs_exam_result by first finding what is the min or max date by for each id_user then join that back on the exam results table.
SELECT
r.correct,
r.items,
r.percentage,
r.date_taken,
u.username,
u.FN,
u.user_course_type,
IFNULL(u.user_major_type,'N/A') as user_major_type,
u.level_name,
u.section_name
FROM bcc_fs_exam_result r
INNER JOIN (
SELECT u.id_user,
u.username,
CONCAT(u.lastname,', ',u.firstname) as FN,
c.user_course_type,
m.user_major_type,
l.level_name,
s.section_name
FROM bcc_fs_user u
LEFT JOIN bcc_fs_user_course c on c.id_user_course = u.id_user_course
LEFT JOIN bcc_fs_user_major m on m.id_user_major = u.id_user_major
LEFT JOIN bcc_fs_group_level l ON l.id_level = u.id_level
LEFT JOIN bcc_fs_group_section s ON s.id_section = u.id_section
) u ON r.id_user = u.id_user
INNER JOIN (
SELECT
id_user, max(r.date_taken) as last_date_taken
FROM bcc_fs_exam_result
GROUP BY id_user
) as lastdate ON lastDate.id_user = r.id_user and r.date_taken = lastdate.last_date_taken
which could be more simply written as :
SELECT
r.correct,
r.items,
r.percentage,
r.date_taken,
u.username,
CONCAT(u.lastname,', ',u.firstname) as FN,
c.user_course_type,
IFNULL(m.user_major_type,'N/A') as user_major_type,
l.level_name,
s.section_name
FROM bcc_fs_exam_result r
INNER JOIN (
SELECT
id_user, max(r.date_taken) as last_date_taken
FROM bcc_fs_exam_result
GROUP BY id_user
) as lastdate ON lastDate.id_user = r.id_user and r.date_taken = lastdate.last_date_taken
INNER JOIN bcc_fs_user u on r.id_user = u.id_user
LEFT JOIN bcc_fs_user_course c on c.id_user_course = u.id_user_course
LEFT JOIN bcc_fs_user_major m on m.id_user_major = u.id_user_major
LEFT JOIN bcc_fs_group_level l ON l.id_level = u.id_level
LEFT JOIN bcc_fs_group_section s ON s.id_section = u.id_section
You have assumed that id_user + date_taken is a surrogate key of bcc_fs_exam_result which you should probably enforce with a constraint if at all possible. Otherwise it looks from your sample data that the order of the unique id column id_result follows the date_takenso you might be better to use Max(id_result) rather than Max(date_taken). That would avoid returning duplicate rows for one id_user where two rows of bcc_fs_exam_result have the same taken_date and `id_user.
I have three tables:
cp_projeto (id, nome...)
cp_habilidade_projeto (id, id_projeto)
cp_habilidade (id, nome...)
I need all projects with all cp_habilidade where cp_projeto have one cp_habilidade. My actual query:
SELECT
p.id as id_projeto,
p.nome as nome_projeto,
p.id_tipo_projeto,
p.dhPostagem,
cp_habilidade_projeto.id as id_habilidade_projeto,
cp_habilidade.nome as nome_habilidade
FROM (
SELECT * FROM cp_projeto
WHERE (id_status_projeto = 2)
ORDER BY dhPostagem DESC LIMIT 0, 10
) AS p
inner JOIN cp_habilidade_projeto ON (cp_habilidade_projeto.id_projeto = p.id)
inner JOIN cp_habilidade ON (cp_habilidade.id = cp_habilidade_projeto.id_habilidade)
JOIN cp_sub_categoria ON (cp_sub_categoria.id = p.id_sub_categoria)
WHERE (
p.nome like '%CSS%'
OR cp_habilidade.nome like '%CSS%'
)
This returns only cp_habilidade.nome = %CSS%, I need it all.
Thanks!
SELECT
p.id as id_projeto,
p.nome as nome_projeto,
p.id_tipo_projeto,
p.dhPostagem,
hp.id as id_habilidade_projeto,
h.nome as nome_habilidade
FROM cp_projecto p
JOIN cp_habilidade_projeto hp ON p.id = hp.id_projeto
JOIN cp_habilidade h ON h.id = hp.id_habilidade
WHERE p.id IN ( SELECT cp_habilidade_projeto.id_projeto
FROM cp_habilidade
JOIN cp_habilidade_projeto ON cp_habilidade.id = cp_habilidade_projeto.id_habilidade
WHERE cp_habilidade.nome LIKE '%CSS%' )
I would like to use JOIN instead of IN in the following SQL query. I can't figure out how to do it.
SELECT * FROM shop_orders WHERE
id IN (SELECT orders_id FROM shop_orders_data WHERE closed='1' /*AND backorder='0'*/ AND exhibition_id='389' AND
exhibition_id IN (SELECT id FROM shop_exhibitions WHERE
country_id IN (SELECT id FROM countries WHERE id='72')) AND in_country = '72' AND
exhibition_id IN (SELECT id FROM shop_exhibitions WHERE start<=1336946400 AND end>1336600800)) AND
id IN (SELECT orders_id FROM shop_orders_products WHERE
products_id IN (SELECT id FROM shop_products WHERE artno='120000' OR name LIKE '%120000%')) AND created>=1333231200 AND created<1333663200 ORDER BY created DESC
I tried this:
SELECT
s.*
FROM
shop_orders s
INNER JOIN shop_orders_data od ON s.id=od.orders_id
INNER JOIN shop_exhibitions se ON od.exhibition_id=se.id
INNER JOIN countries co ON se.country_id=co.id
INNER JOIN shop_orders_products sop ON s.id=sop.orders_id
INNER JOIN shop_products sp
ON sop.products_id=sp.id
WHERE od.closed=1
AND ( sp.artno='120000' or sp.name LIKE '%120000%' )
AND ( od.exhibition_id='389')
AND ( od.in_country = '72')
AND ( se.start <=1336946400)
AND ( se.end >1336600800)
AND ( se.created>=1333231200)
AND ( se.created<1333663200)
ORDER BY `s`.`created` DESC
I this correct??
See if this works (and study the code to learn how it works):
SELECT *
FROM shop_orders so
JOIN shop_orders_data sod ON (
(so.id = sod.orders_id)
AND (sod.closed = '1')
/*AND (sod.backorder = '0') */
AND (sod.exhibition_id = '389')
AND (sod.in_country = '72')
)
JOIN shop_exhibitions se ON (
(sod.exhibition_id = se.id)
AND (se.start <= 1336946400)
AND (se.end > 1336600800)
)
JOIN countries c ON (
(se.country_id = c.id)
AND (c.id = '72')
)
JOIN shop_orders_products sop ON (
(so.id = sop.orders_id)
)
JOIN shop_products sp ON (
(sop.products_id = sp.id)
AND ((sp.artno='120000') OR (sp.name LIKE '%120000%'))
)
WHERE (so.created >= 1333231200) AND (so.created < 1333663200)
ORDER BY so.created DESC;
The join syntax works like this:
SELECT field1,field2,field3
FROM FirstTable
JOIN SecondTable ON (FirstTable.PrimaryKey = SecondTable.ForeignKey)
JOIN ThirdTable ON (FirstTable.PrimaryKey = ThirdTable.ForeignKey)
Try applying this approach to your query.
I have a mysql query which is as follows
(SELECT order_product.op_id,
order_product.ocat_id,
order_product.op_partnunber,
order_product.op_name,
order_product.op_upc,
order_product.op_desc,
order_stockavailable.osa_id,
order_stockavailable.of_id,
order_stockavailable.osa_stocka,
order_category.ocat_name
FROM
order_product
LEFT JOIN order_category
ON order_product.ocat_id = order_category.ocat_id
LEFT JOIN order_stockavailable
ON order_product.op_id = order_stockavailable.op_id)
UNION
(SELECT order_product.op_id,
order_product.ocat_id,
order_product.op_partnunber,
order_product.op_name,
order_product.op_upc,
order_product.op_desc,
order_stockavailable_attributes.id,
order_stockavailable_attributes.of_id,
order_stockavailable_attributes.opap_stock,
order_category.ocat_name
FROM order_product
LEFT JOIN order_category
ON order_product.ocat_id = order_category.ocat_id
LEFT JOIN order_stockavailable
ON order_product.op_id = order_stockavailable.op_id
LEFT JOIN order_stockavailable_attributes
ON order_product.op_id = order_stockavailable_attributes.op_id)
ORDER BY order_product.op_name
The query is givng error, T
Table 'order_product' from one of the SELECTs cannot be used in global ORDER clause
I checked the MYSQL manual, but am not getting any clue, any help would be really great.
SELECT *
FROM (
SELECT order_product.op_id,
order_product.ocat_id,
order_product.op_partnunber,
order_product.op_name,
order_product.op_upc,
order_product.op_desc,
order_stockavailable.osa_id,
order_stockavailable.of_id,
order_stockavailable.osa_stocka,
order_category.ocat_name
FROM
order_product
LEFT JOIN order_category
ON order_product.ocat_id = order_category.ocat_id
LEFT JOIN order_stockavailable
ON order_product.op_id = order_stockavailable.op_id
UNION
SELECT order_product.op_id,
order_product.ocat_id,
order_product.op_partnunber,
order_product.op_name,
order_product.op_upc,
order_product.op_desc,
order_stockavailable_attributes.id,
order_stockavailable_attributes.of_id,
order_stockavailable_attributes.opap_stock,
order_category.ocat_name
FROM order_product
LEFT JOIN order_category
ON order_product.ocat_id = order_category.ocat_id
LEFT JOIN order_stockavailable
ON order_product.op_id = order_stockavailable.op_id
LEFT JOIN order_stockavailable_attributes
ON order_product.op_id = order_stockavailable_attributes.op_id
) t
ORDER BY op_name
Btw: there is no need to put the individual SELECTs of a UNION into brackets.
Try the following syntax?
SELECT
x, y
FROM
(
SELECT x, y FROM z
UNION
SELECT a, b FROM c
)
ORDER BY
x, y
Try
order by op_name
instead of
ORDER BY order_product.op_name
SELECT *
FROM (SELECT order_product.op_id,
...
FROM
... )
UNION
(SELECT order_product.op_id,
...
FROM order_product
... ) U
ORDER BY op_name;