SELECT * FROM `tbl_wines`
LEFT JOIN `tbl_wines_attrib`
ON `tbl_wines`.`intWinesID` = `tbl_wines_attrib`.`intWinesID`
AND `tbl_wines_attrib`.`intAttributeValueId` = 4
WHERE `intStatus` = 1
LIMIT 0 , 20
I need to know if this query is correct. But am getting all the values from tbl_wines not only the value where intAttributeValueId = 4.
Can anyone help out?
It looks like that condition has been placed as part of your JOIN rather than a WHERE condition. Instead try the following, moving tbl_wines_attrib.intAttributeValueId =4into the WHERE clause.
SELECT * FROM `tbl_wines` LEFT JOIN `tbl_wines_attrib` ON `tbl_wines`.`intWinesID` = `tbl_wines_attrib`.`intWinesID` WHERE `tbl_wines_attrib`.`intAttributeValueId` = 4 AND `intStatus` =1 LIMIT 0 , 20
Related
I am using the following query to get COUNT items from rows from the same table in LEFT JOIN.
This is the query:
SELECT
pac.id_sat as id_sat,
pac.nombre_contacto as nombre_contacto,
pac.centro_contacto as centro_contacto,
pac.tel_contacto as tel_contacto,
pac.horario_contacto as horario_contacto,
pac.email_contacto as email_contacto,
pac.num_factura as num_factura,
pac.fecha_factura as fecha_factura,
eq.nombre_equipo as modelo_equipo,
pac.num_serie as num_serie,
pac.tipo_incidencia as tipo_incidencia,
pac.cod_sat as cod_sat,
pac.estado as estado,
pac.clinica as clinica,
pac.fecha_sat as fecha_sat,
COUNT(medfotos.id_media_sat) as num_fotos,
COUNT(medvideos.id_media_sat) as num_videos
FROM tb_sat pac
LEFT JOIN tb_equipos eq ON pac.modelo_equipo = eq.id_equipo
LEFT JOIN tb_media_sat medfotos ON pac.cod_sat = medfotos.cod_sat AND medfotos.tipo = 1
LEFT JOIN tb_media_sat medvideos ON pac.cod_sat = medvideos.cod_sat AND medvideos.tipo = 2
WHERE pac.clinica = '".$idclinica."'
GROUP BY pac.id_sat
ORDER BY pac.fecha_sat DESC
My issue is that I am getting a wrong amount of COUNT items.
The real value for num_fotos should be 3 and for num_videos should be 2.
I am getting num_fotos = 6 and num_videos = 6.
EDIT
Table tb_sat
Table tb_media_sat
Sub-query will work better in your case like as follows:
SELECT pac.*, (SELECT COUNT(id_media_sat) FROM tb_media_sat WHERE cod_sat=pac.cod_sat AND tipo=1) AS num_fotos, (SELECT COUNT(id_media_sat) FROM tb_media_sat WHERE cod_sat=pac.cod_sat AND tipo=2) AS num_videos FROM tb_sat pac WHERE pac.clinica = '".$idclinica."' ORDER BY pac.fecha_sat DESC
Rest columns, please add yourself slowly slowly. I hope you will get correct output.
The following query returns what is displayed in the attached image.
What I thought would be very simple has turned out to be very complex. All I simply want to do is now total up the result into one row and column. In this case the sum would be 161. How do I do this? I've literally tried everything. I hope I've provided enough information.
SELECT
TRUNCATE
(
SUM(
`assignment`.`percentage_achieved` *(
SELECT
`unitComponentWeighting`.`percentage_weighting` / 100
FROM
`unitComponentWeighting`
WHERE
`assignment`.`assignment_component_id` = `unitComponentWeighting`.`component_lookup_id`
LIMIT 1
)
),
2
) AS `unit_percentage_grade`
FROM
`assignment`
LEFT JOIN `assignmentType` ON `assignment`.`assignment_type_id` = `assignmentType`.`id`
LEFT JOIN `assignmentComponentLookup` ON `assignmentComponentLookup`.`id` = `assignment`.`assignment_component_id`
LEFT JOIN `unit` ON `unit`.`id` = `assignment`.`unit_id`
LEFT JOIN `assignmentSequence` ON `assignmentSequence`.`id` = `assignment`.`assignment_sequence_id`
LEFT JOIN `yearGroup` ON `yearGroup`.`id` = `unit`.`year_group_id`
WHERE
`yearGroup`.`id` = 1
GROUP BY
`assignment`.`unit_id`
try removing this part
" GROUP BY
assignment.unit_id "
I have the following Database Design:
Database Design
I want to get all Information from table 'info' where the id IS NOT in table 'archived'. To do so I wrote:
SELECT *
FROM traffic_info i
LEFT JOIN
traffic_info_archived a ON (i.info_id = a.info_id)
WHERE
i.branch_id = 4 AND i.user_id = 7 a.info_id IS NULL ORDER BY i.info_date_from ASC
This works as expected.
The next challenge is to only show information that are also included in the 'published' table. To get this done I have expanded my previous query to :
SELECT *
FROM traffic_info i
LEFT JOIN
traffic_info_archived a ON (i.info_id = a.info_id)
RIGHT JOIN
traffic_info_publised p ON (i.info_id = p.info_id)
WHERE
i.branch_id = 4 AND a.info_id AND i.user_id = 7 IS NULL ORDER BY i.info_date_from ASC
This does also work as expected.
The final challenge is to Order this result according to table 'read'. Information´s id that are NOT in table 'read' should be ordered ASC. But even if its id does not appear in table 'read' they should not be excluded from the query output. BUT the primary ORDER should be
i.info_date_from ASC
I hope this is understandable, my English is not the best :) If not, please comment and I will do my best to make it understandable. Hope some can help!
I´ve tried to create a SQLFiddle, but I wasn´t able to create a runnable example, sorry for that.
UPADTE:
Using the approach from #Dylan Su
SELECT *
FROM traffic_info i
LEFT JOIN
traffic_info_archived a ON (i.info_id = a.info_id)
INNER JOIN
traffic_info_publised p ON (i.info_id = p.info_id)
WHERE
i.branch_id = 4 AND a.info_id AND i.user_id = 7 IS NULL
ORDER BY
CASE WHEN NOT EXISTS(SELECT 1 FROM read WHERE i.info_id = read.info_id)
THEN i.info_date_from END ASC;
the goal is nearer then it ever was :)
Sample Data output
Both entries marked with a red "X" are in table read. Therefore id 3 should be last the, in the middle 1 and 2 at the top.
So the last thing to archive is to do the correct order of table read. I´ve tried sth like:
(SELECT 1 FROM traffic_info_read WHERE i.info_id = traffic_info_read.info_id ORDER BY traffic_info_read.info_id DESC)
But that didn´t had any influnce.
Try this:
SELECT *
FROM traffic_info i
LEFT JOIN
traffic_info_archived a ON (i.info_id = a.info_id)
INNER JOIN
traffic_info_publised p ON (i.info_id = p.info_id)
WHERE
i.branch_id = 4 AND a.info_id AND i.user_id = 7 IS NULL
ORDER BY
EXISTS(SELECT 1 FROM read WHERE i.info_id = read.info_id) ASC,
i.info_date_from ASC;
The Answer of #Dylan Su is absolutely correct and I won´t unmark it as accepted.
However, based on the Information I gained from the conversation I have created another solution, that doesn´t make use of sub query.
I heard that using just JOIN´s will result in better performance, I don´t know if it´s correct and I don´t have that much test data currently to find out, but here is the solution using no sub query.
SELECT *
FROM traffic_info i
LEFT JOIN
traffic_info_archived a ON (i.info_id = a.info_id)
INNER JOIN
traffic_info_published p ON (i.info_id = p.info_id)
LEFT JOIN
traffic_info_read r ON (i.info_id = r.info_id)
WHERE
i.branch_id = 4 AND a.info_id IS NULL ORDER BY r.info_id IS NULL DESC, i.info_date_from ASC
;
I'm pulling information out with the code below, which works absolutely fine.
I need to pull out the data without the contractorID affecting it, then subtract the rows from the first query from it.
SELECT
DISTINCT a.addID
, p.propdetailID
, p.plotID
FROM address AS a
LEFT JOIN propdetail AS p ON (a.addID = p.addID)
JOIN housebuildertoproperty AS htp ON (htp.addID = p.addID)
JOIN contractortopropdetail AS ctp ON (ctp.propdetailID = p.propdetailID)
WHERE htp.housebuilderID = 1
AND ctp.contractorID = 1
So what I mean is:
row 1
that's the result from the first query
row 1
row 2
row 3
That's the result from the query without the AND ctp.contractorID = 1 part. I need code that will pull out:
row 2
row 3
I tried MINUS but phpMyAdmin just didn't accept it. I've looked into NOT IN but I can't seem to figure this out.
First, the left join is unnecessary, because the subsequent join conditions and where clause turn it into an inner join.
Your problem is that some of the addresses on with contractorID = 1 have other contractors. The solution is to use group by instead of distinct and use a having clause to filter out addresses for contractor 1:
SELECT a.addID, p.propdetailID, p.plotID
FROM address a JOIN
propdetail p
ON (a.addID = p.addID) JOIN
housebuildertoproperty htp
ON (htp.addID = p.addID) JOIN
contractortopropdetail ctp
ON (ctp.propdetailID = p.propdetailID)
WHERE htp.housebuilderID = 1
GROUP BY a.addID, p.propdetailID, p.plotID
HAVING SUM(ctp.contractorID = 1) = 0
I've got a complicated problem.
How can I force MySQL to replace the first "command.deagle2" (Mode 1) with the second "command.deagle2" (Mode 0) ?
I simply show you the code and I hope you can help me.
Here my code:
SELECT DISTINCT
`right`.`name` AS `Right`,
1 AS `Mode`
FROM
`user`
INNER JOIN
`user_group` ON
`user`.`id` = `user_group`.`user_id`
INNER JOIN
`group` ON
`user_group`.`group_id` = `group`.`id`
INNER JOIN
`group_right` ON
`group`.`id` = `group_right`.`group_id`
INNER JOIN
`right` ON
`group_right`.`right_id` = `right`.`id`
WHERE
`user`.`username` = 'Dominik'
UNION
SELECT DISTINCT
`right`.`name` AS `Right`,
`user_right`.`mode` AS `Mode`
FROM
`user`,
`right`,
`user_right`
WHERE
`user`.`id` = `user_right`.`user_id` AND
`right`.`id` = `user_right`.`right_id` AND
`user`.`username` = 'Dominik'
This query returns the following results:
Right | Mode
-----------------------
command.deagle | 1
command.deagle2 | 1
command.gmx | 1
command.givegun | 1
command.deagle2 | 0
Sample dataset: http://pastebin.com/m5LHsDRi
I already saw that there is an REPLACE keyword, but I dont really know how to use it properly.
Thanks for your Time.
Dominik
I take it your "mode" column constitutes a priority, and you want the only the distinct value of "Right" with the lowest-numbered priority in your result set.
Try wrapping this query around the query you gave us:
SELECT Right,
MIN(Mode) AS Mode,
FROM (
/* your big query */
) AS q
GROUP BY Right
That will give you what you want. By the way, you can remove the DISTINCT keyword from your main query if you do this; the GROUP BY will fill the same purpose.