I have 3 SQL tables.
Games wo_tenis_partidos
Selections wo_tenis_pronosticos
Quotas wo_tenis_cuotas_ha2
I need to get all rows with two conditions:
Condition 1: where CUOTA1 >= CUOTAMIN1 or CUOTA2 >= CUOTAMIN2
Condition 2: Add all rows with same ID_PARTIDO that appeared in Condition 1
The condition 1 is easy:
SELECT *
FROM wo_tenis_partidos a
INNER JOIN wo_tenis_cuotas_ha2 c ON c.id_partido = a.id
INNER JOIN wo_tenis_pronosticos p ON p.id_partido = a.id
WHERE
a.resultado IS NULL
AND (c.cuota1 >= p.cuotamin1 OR c.cuota2 >= p.cuotamin2)
But to add the condiciton 2 I need to do this:
SELECT *
FROM wo_tenis_partidos a
INNER JOIN wo_tenis_cuotas_ha2 c ON c.id_partido = a.id
INNER JOIN wo_tenis_pronosticos p ON p.id_partido = a.id
WHERE a.id IN (
SELECT a.id
FROM wo_tenis_partidos a
INNER JOIN wo_tenis_cuotas_ha2 c ON c.id_partido = a.id
INNER JOIN wo_tenis_pronosticos p ON p.id_partido = a.id
AND a.resultado IS NULL
AND (c.cuota1 >= p.cuotamin1 OR c.cuota2 >= p.cuotamin2)
)
It's the correct way? there is another more optimized alternative?
No need of INNER JOIN in first query and use EXISTS instead of IN clause for better performance
SELECT *
FROM wo_tenis_partidos aa
INNER JOIN wo_tenis_cuotas_ha2 c ON c.id_partido = aa.id
INNER JOIN wo_tenis_pronosticos p ON p.id_partido = aa.id
WHERE EXISTS (
SELECT 1
FROM wo_tenis_partidos a
INNER JOIN wo_tenis_cuotas_ha2 c ON c.id_partido = a.id
INNER JOIN wo_tenis_pronosticos p ON p.id_partido = a.id
AND a.resultado IS NULL
AND (c.cuota1 >= p.cuotamin1 OR c.cuota2 >= p.cuotamin2)
WHERE aa.id =a.id
)
Related
I have this MySQL query:
SELECT * FROM A
LEFT JOIN B
ON B.id = A.id
LEFT JOIN C
ON C.other = A.other
WHERE
(SELECT SUM(C.quantity) FROM C WHERE C.other = A.other) < A.quantity
Is any way to get values from SELECT from WHERE clause? For example for PHP to use? Something like:
WHERE
(SELECT SUM(C.quantity) FROM C WHERE C.other = A.other) AS quantity < A.quantity
I need to compare this two values in mysql and then i need them both in my script. I can of course put this subquery into first SELECT - but i belive there is better way to do this than repeat code in many places.
You could do that as follows:
SELECT A.*, B.*, C.*, D.sumq
FROM A
LEFT JOIN B
ON B.id = A.id
LEFT JOIN C
ON C.other = A.other
LEFT JOIN (
SELECT other, SUM(quantity) sumq
FROM C
GROUP BY other
) AS D
ON D.other = A.other
WHERE COALESCE(D.sumq, 0) < A.quantity
I want to get all the detail about my purchase from the following tables,
1) tbl_track
2) tbl_album
3) tbl_purchase
tbl _purchase table :-
item_id
type
price
tbl_track table :-
id
title
description
tbl_album table :-
id
title
description
Now based on type of tbl_purchase I want to join one of table,
It means If type = 0 then LEFT JOIN with tbl_track and for type = 1 then LEFT JOIN with tbl_album.
I want to do these with one query only, It is easily done with two query.
Thanks in advance.
With UNION:
SELECT p.type, t.id, t.title, t.description
FROM tbl_purchase p
JOIN tbl_track t ON t.id = p.item_id AND p.type = 0
UNION
SELECT p.type, a.id, a.title, a.description
FROM tbl_purchase p
JOIN tbl_album a ON a.id = p.item_id AND p.type = 1
I think you need left join both tables in any case. So you can use next query for example
SELECT P.*,
T.id AS track_id,
A.id AS album_id
FROM tbl_purchase AS P
LEFT JOIN tbl_track AS T ON P.item_id = T.id
AND P.type = 0
LEFT JOIN tbl_album AS A ON P.item_id = A.item_id
AND P.type = 1
Try this:
SELECT *
FROM tbl_purchase A
LEFT OUTER JOIN tbl_track B ON A.item_id = B.id AND A.type = 0
LEFT OUTER JOIN tbl_album C ON A.item_id = C.id AND A.type = 1;
i dont't know how to select row with max column value group by another column. I have T-SQL
CREATE PROC GET_USER
AS
BEGIN
SELECT A.USER_ID, B.START_DATE , D.START_DATE, A.FULL_NAME,A.COST_CENTER,
F.DEPARTMENT_NAME,G.BU_NAME
FROM USERS A INNER JOIN USER_PERSON B ON A.USER_ID=B.USER_ID
INNER JOIN TYPE_PERSON C ON C.TYPE_PERSON_ID = B.TYPE_PERSON_ID
INNER JOIN USER_TRANSACTION D ON D.USER_ID = A.USER_ID
INNER JOIN TRANSACTIONS E ON E.TRANSACTION_ID = D.TRANSACTION_ID
INNER JOIN DEPARTMENT F ON F.DEPARTMENT_ID = D.DEPARTMENT_ID
INNER JOIN BUS_UNIT G ON G.BU_ID = D.BU_ID
INNER JOIN BRANCH H ON H.BRANCH_ID = D.BRANCH_ID
INNER JOIN POSITION J ON J.POSITION_ID = D.POSITION_ID
WHERE A.FLAG = 'TRUE'
END
the result will select max(B.START_DATE) and max(D.START_DATE) and Group by USER_ID
try this
SELECT T.USER_ID ,
MAX(T.START_DATE) AS [Max First Start Date] ,
MAX(T.[Second Start Date]) AS [Max Second Start Date]
FROM ( SELECT A.USER_ID ,
B.START_DATE ,
D.START_DATE AS [Second Start Date] ,
A.FULL_NAME ,
A.COST_CENTER ,
F.DEPARTMENT_NAME ,
G.BU_NAME
FROM USERS A
INNER JOIN USER_PERSON B ON A.USER_ID = B.USER_ID
INNER JOIN TYPE_PERSON C ON C.TYPE_PERSON_ID = B.TYPE_PERSON_ID
INNER JOIN USER_TRANSACTION D ON D.USER_ID = A.USER_ID
INNER JOIN TRANSACTIONS E ON E.TRANSACTION_ID = D.TRANSACTION_ID
INNER JOIN DEPARTMENT F ON F.DEPARTMENT_ID = D.DEPARTMENT_ID
INNER JOIN BUS_UNIT G ON G.BU_ID = D.BU_ID
INNER JOIN BRANCH H ON H.BRANCH_ID = D.BRANCH_ID
INNER JOIN POSITION J ON J.POSITION_ID = D.POSITION_ID
WHERE A.FLAG = 'TRUE'
) AS T
GROUP BY T.USER_ID
I'm facing an issue where I have several tables joined, but I need to combine the e.url and f.url_new columns. Otherwise, the result is as expected. Here is my query.
SELECT a.`added_date`,b.`type`,c.`action`,e.`url`,f.`url_new`
FROM `websites_submitted_main` a
INNER JOIN `websites_submitted_type` b ON a.`type_id` = b.`type_id`
INNER JOIN `websites_submitted_action` c ON a.`action_id` = c.`action_id`
INNER JOIN `users` d ON a.`user_id` = d.`user_id`
LEFT JOIN `websites` e ON a.`url_id` = e.`id`
LEFT JOIN `websites_submitted_new` f ON a.`url_new_id` = f.`url_new_id`
WHERE a.`user_id` = 1 ORDER BY a.`added_date` DESC
I've tried CONCAT, but the column contained all NULL values. Here's that query.
SELECT a.`added_date`,b.`type`,c.`action`,CONCAT(e.`url`,f.`url_new`) AS url
FROM `websites_submitted_main` a
INNER JOIN `websites_submitted_type` b ON a.`type_id` = b.`type_id`
INNER JOIN `websites_submitted_action` c ON a.`action_id` = c.`action_id`
INNER JOIN `users` d ON a.`user_id` = d.`user_id`
LEFT JOIN `websites` e ON a.`url_id` = e.`id`
LEFT JOIN `websites_submitted_new` f ON a.`url_new_id` = f.`url_new_id`
WHERE a.`user_id` = 1 ORDER BY a.`added_date` DESC
Is there a minor modification I can make to this query to merge these columns?
try using CONCAT_WS() instead of CONCAT()
SELECT `CONCAT_WS(' ',e.url, f.url_new )` ....
CONCAT_WS() will concatenate the values if the values are not null.
From the manual of CONCAT()
CONCAT() returns NULL if any argument is NULL.
SELECT a.`added_date`,b.`type`,c.`action`,CONCAT_WS("", e.`url`,f.`url_new`) AS url
FROM `websites_submitted_main` a
INNER JOIN `websites_submitted_type` b ON a.`type_id` = b.`type_id`
INNER JOIN `websites_submitted_action` c ON a.`action_id` = c.`action_id`
INNER JOIN `users` d ON a.`user_id` = d.`user_id`
LEFT JOIN `websites` e ON a.`url_id` = e.`id`
LEFT JOIN `websites_submitted_new` f ON a.`url_new_id` = f.`url_new_id`
WHERE a.`user_id` = 1 ORDER BY a.`added_date` DESC
How can I select certain columns from the second and third tables using INNER JOIN
SELECT
*
FROM
1_packages_plu AS p
INNER JOIN
1_stock as s ON p.fk_products_id = s.fk_products_id
AND branch = 1
INNER JOIN
1_products AS j ON p.fk_products_id = j.id
WHERE
fk_packages_id = 54;
In the tables 1_stock I only want to return the value of stock-repair columns and in the 1_products all I need is make,model columns
SELECT
p.* -- All columns from p
,
s.columnName -- Just that column from s
,
j.columnName -- And just that column from j
FROM
1_packages_plu AS p
INNER JOIN 1_stock as s
ON p.fk_products_id = s.fk_products_id
AND
branch = 1
INNER JOIN 1_products AS j
ON p.fk_products_id = j.id
WHERE
fk_packages_id = 54
You need (.) operator to access column:
SELECT
p.* ,
s.stock-repair,
j.make, j.model
FROM
1_packages_plu AS p
INNER JOIN 1_stock as s
ON p.fk_products_id = s.fk_products_id
AND branch = 1
INNER JOIN 1_products AS j
ON p.fk_products_id = j.id
WHERE
fk_packages_id = 54
ORDER BY p.colunmname
;
SELECT Table1.*, Table2.FK, Table2.SomeColumn, Table2.SomeColumn, Table3.SomeColumn, Table3.SomeColumn
FROM
Table1 INNER JOIN
Table2 ON Table1.FK = Table2.Table1FK INNER JOIN
Table3 ON Table2.FK = Table3.Table2FK