I have this table
What I want to do is to query/echo out those id with the same value for example id 1 and id 3 has the same value. Is that possible in the same table and column? Thanks!
You can use a self-join to get all pairs of IDs with the same weight
SELECT t1.weight, t1.id AS id1, t2.id AS id2
FROM YourTable AS t1
JOIN YourTable AS t2 ON t1.weight = t2.weight AND t1.id < t2.id
or GROUP_CONCAT to get all the IDs with the same weight in a single row:
SELECT weight, GROUP_CONCAT(id) AS ids
FROM YourTable
GROUP BY weight
HAVING COUNT(*) > 1
To get just one pair for each weight:
SELECT t1.weight, t1.id AS id1, t2.id AS id2
FROM YourTable AS t1
JOIN YourTable AS t2 ON t1.weight = t2.weight AND t1.id < t2.id
GROUP BY t1.weight
DEMO
Related
I searched around and found a near example to what I'm looking for, but it doesn't work in my case.
I have a query that does an INNER JOIN on two tables and this join constrains my overall data set substantially. I then want to LEFT JOIN onto a third table but I only want one record from that third table. The reason for the left join is because not every result of the INNER JOIN has a match in the 3rd table. Something like this:
SELECT DISTINCT t1.code, t2.id, t2.code, t3.id, t3.source_title, t3.display_order
FROM table1 t1
INNER JOIN table2 t2 ON t2.code=t1.code AND t2.type=0
LEFT JOIN table3 t3 ON t3.code=t1.code
ORDER BY t1.code, t3.display_order
This query returns too many records because the third table contains multiple records with a matching code. I just want the first one that matches with the lowest display_order value and, unfortunately, I can't limit the records to have display_order=1 because the lowest display order is not always one.
IMPORTANT: The t3.id value (if any) returned by this query must correspond to the record with the lowest display_order value. I.e., it won't work if the query correctly returns the lowest display_order value but the t3.id value corresponds to some other record in table 3.
Is this even possible? Any help would be much appreciated.
EDIT: Per Nick's suggestion, I have tried this, which appears to be working. I'll do some verification and report back:
SELECT DISTINCT t1.code, t2.*, sq.id, sq.source_title, sq.display_order
FROM table1 t1
INNER JOIN table2 p ON t2.code=t1.code AND t2.type=0
LEFT JOIN (
SELECT t3.*
FROM table3 t3
WHERE t3.display_order=(
SELECT MIN(display_order)
FROM table3 t3a
WHERE t3a.code = t3.code
)
) sq ON sq.code=t1.code
ORDER BY t1.code, sq.display_order
You should be able to replace table3 in your LEFT JOIN with
(SELECT *
FROM table3 t3
WHERE display_order = (SELECT MIN(display_order)
FROM table3 t3a
WHERE t3a.code = t3.code)
) t3
In MySQL 8.0 you can try to use row_number() for each code and ordered by display_order in a subquery from table3. Then left join that result and check for the row_number() to be equal to 1.
SELECT DISTINCT
t1.code,
t2.id,
t2.code,
t3.id,
t3.source_title,
t3.display_order
FROM table1 t1
INNER JOIN table2 t2
ON t2.code = t1.code
LEFT JOIN (SELECT t3.id,
t3.source_title,
t3.display_order,
t3.code,
row_number() OVER (PARTITION BY t3.code
ORDER BY t3.display_order) rn
FROM table3 t3) t3
ON t3.code = t1.code
WHERE t2.type = 0
AND t3.rn = 1
ORDER BY t1.code,
t3.display_order;
In lower versions you can try correlated subqueries ordered by display_order and LIMIT 1 (to get only one record).
SELECT DISTINCT
t1.code,
t2.id,
t2.code,
(SELECT t3.id
FROM table3 t3
WHERE t3.code = t1.code
ORDER BY t3.display_order,
t3.id
LIMIT 1) id,
(SELECT t3.source_title
FROM table3 t3
WHERE t3.code = t1.code
ORDER BY t3.display_order,
t3.id
LIMIT 1) source_title,
(SELECT t3.display_order
FROM table3 t3
WHERE t3.code = t1.code
ORDER BY t3.display_order,
t3.id
LIMIT 1) display_order
FROM table1 t1
INNER JOIN table2 t2
ON t2.code = t1.code
WHERE t2.type = 0
ORDER BY t1.code,
(SELECT t3.display_order
FROM table3 t3
WHERE t3.code = t1.code
ORDER BY t3.display_order,
t3.id
LIMIT 1);
I assumed, that display_order in table3 isn't unique but id is. So I added id to the ORDER BY clauses in the subqueries to make sure the same record is selected in each of them. If display_order is unique, you can remove id FROM the ORDER BY clauses.
Edit:
If you don't want to repeat the subqueries in the (overall) ORDER BY clause, you can also order by the column ordinals. E.g.:
...
ORDER BY 1, 6;
This query select all from my first table where the row id doesnt exist in my second table:
SELECT *
FROM table1
WHERE NOT EXISTS (SELECT idTable1 FROM table2 WHERE table1.id=idTable1
This query select all row of table1 who exists in the table2 with the last id of table2 ( cause one row of table1 can have multiple row of the table2 but only the last is not forget ):
SELECT *
FROM table2
INNER JOIN table 1 ON idTable1 = table1.id
WHERE table2.id IN (SELECT MAX(actioncur.id) FROM table2 GROUP BY idTable1)
I want to regroup the both query in one, I want to select all row of table1 when id doesnt exist in table2 and select all of the table1 for the last table2 id. For exemple i want to select that : row 1 -> id=44 ; table2.id= 187 ; idTable1=44. Row 2 ->id=45 ; table2.id=? ; idTable1=?
If I understand correctly, you can use a correlated subquery:
SELECT t1.*,
(SELECT MAX(t2.id)
FROM table2 t2
WHERE t1.id = t2.idTable1
) as max_t2id
FROM table1 t1;
You can also do this with LEFT JOIN and GROUP BY:
SELECT t1.*, MAX(t2.id) as max_t2id
FROM table1 t1 JOIN
table2 t2
ON t1.id = t2.idTable1
GROUP BY t1.id;
I want to create a select query using join on 3 or more tables. I have 3 tables namely t1, t2, t3 and a common column id existing in all 3 tables. I want to select the 3 table if the id exists in the table my query is like this.
Select * from t1
inner join t2 on t1.id = t2.id
inner join t3 on t2.id = t3.id
where t1.id = 1 and t2.id = 1 and t3.id = 1
the query is returning values if the id exists in all the 3 tables. But if it is not in any table example t3 i will not return anything. I am looking for a way that if it does not exist in t3 it should i proceed to just select from t1 and t2
Is this what you need?
SELECT *
FROM t1
INNER JOIN t2 on t1.id = t2.id
LEFT JOIN t3 on t2.id = t3.id
WHERE t1.id = 1 AND t2.id = 1 AND (t3.id = NULL OR t3.id = 1)
SELECT * FROM t1 JOIN t2 ON t1.id = t2.id
LEFT JOIN t2 ON t1.id= t2.id WHERE t1.id= 1
This will return null for t3 columns if id is not present in t3
I think these sql wil helpful to you
Select * from t1,t2,t3
where t1.id = 1 or t2.id = 1 0r t3.id = 1
these sql also will useful to you
SELECT * FROM t1 WHERE t1.id=1
UNION ALL
SELECT * FROM t2 WHERE t2.id=1
UNION ALL
SELECT * FROM t2 WHERE t2.id=1
Thank you.
I've a somewhat suboptimal database structure I have to work with. Table1 contains 2 Ids (lets call them id1, id2). These Ids link to 2 rows in another table Table2. I want to get some columns of Table1 and use id1 and id2 to get columns out of Table2. Do I really have to join the same table twice to link to different rows or is there another more efficient way to do this? My queries take over 30 seconds for 20 rows.
SELECT t1.id1, t1.id2, t2.name, t3.name
FROM Table1 t1, Table2 t2, Table2 t3
WHERE t1.id1 = t2.id AND t1.id2 = t3.id AND t1.index = 2
Not tested, but it should be possible to use JOINS:
SELECT t1.id1, t1.id2, t2.name FROM
FROM Table1 t1
JOIN Table2 t2
ON t1.id1 = t2.id OR t1.id2 = t2.id
WHERE t1.index = 2
If you really need this Many-to-Many relation, please try JOIN:
SELECT t2.id AS t2_id,
t3.id AS t3_id,
t2.name AS t2_name,
t3.name AS t3_name
FROM Table1 t1
JOIN Table2 t2
ON t1.id1 = t2.id
JOIN Table3 t3
ON t1.id2 = t3.id
WHERE t1.index = 2
I am trying to count row numbers on an another table and use HAVING to select only rows greater than 0.
SELECT COUNT(t3.ID),t1.ID,t2.sell,t1.date
FROM `album` t1
INNER JOIN `members` t2 ON t2.aID = t1.ID
INNER JOIN `table` t3 ON t3.rID = t1.ID
WHERE t1.date <= '$dt' AND t2.sell = '1'
ORDER BY t1.date DESC
HAVING COUNT(t3.ID) > 0
It doesnt work. where am i wrong ?
Thanks
You need a GROUP BY clause so that MySQL knows how to group the data when you're using aggregate functions such as COUNT.
SELECT COUNT(t3.ID),t1.ID,t2.sell,t1.date
FROM `album` t1
INNER JOIN `members` t2 ON t2.aID = t1.ID
INNER JOIN `table` t3 ON t3.rID = t1.ID
WHERE t1.date <= '$dt' AND t2.sell = '1'
GROUP BY t1.ID,t2.sell,t1.date
HAVING COUNT(t3.ID) > 0
ORDER BY t1.date DESC
For more information, see this.