Insert Join with limit - Mysql - php

How do I go about doing an update with a join while using a limit
Error - Illegal use of limit operator
UPDATE
table1
INNER JOIN table2 ON table1.id = table2.id
SET
table1.field = 'your_value'
WHERE
table1.id = table2.id
LIMIT 1500

Possible solution, it's a bit of a hack
SET #tmp_c = 0;
UPDATE
table1
INNER JOIN table2 ON table1.id = table2.id
INNER JOIN table3 ON table2.id = table3.id
SET
table1.remove_date = NOW() - INTERVAL 5 MONTH
WHERE
table1.active = 1
AND
(IF (table1.active = 1, #tmp_c := #tmp_c + 1 , 0 )) //This is to only increment the counter if the whole where matches
AND
#tmp_c <= 15 //Start ignoring row items as soon as it exceeds the limit amount
;

if you want limit you should use a subselect and join this
UPDATE table1
INNER JOIN ( select * from table1
INNER JOIN table2 ON table1.id = table2.id
LIMIT 1500 ) t on t.id = table1.id
SET table1.field = 'your_value'

Related

MySQL two-table INNER JOIN , LEFT JOINED onto third table with only one row with lowest value

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;

How to check id of one table to another table?

How to find whether distinct value id of one table exits to another tables or not using php.
For example: Let table1 have id (not primary or unique) 4, 5, 6, 4, 6, how to check if id of table2 have 4, 5, 6 or not ?
$query_check= "select distinct(project_code) from table1";
$projects_check = mysql_query($query_check);
while ($result_check = mysql_fetch_array($projects_check)){
$all[] = $result_check[0];
}
$query = "select id from table2";
$projects = mysql_query($query);
while ($result = mysql_fetch_array($projects)){
foreach($all as $all_val){
$pro_code = $result[0];
if($pro_code != $all_val){ }
}
}
This SQL may help:
SELECT table2.id
FROM table2
WHERE table2.id NOT IN (
SELECT DISTINCT table1.id
FROM table1
)
SELECT table2.id
FROM table2
WHERE table2.id IN (
SELECT DISTINCT table1.id
FROM table1
)
To get a list of all the project codes from table1 that are ids in table2:-
SELECT DISTINCT table1.project_code
FROM table1
INNER JOIN table2
ON table1.project_code = table2.id
To get a list of all the project codes from table1 that are NOT ids in table2
SELECT DISTINCT table1.project_code
FROM table1
LEFT OUTER JOIN table2
ON table1.project_code = table2.id
WHERE table2.id IS NULL
If it is the entries on 2 that you want that are not on 1
SELECT DISTINCT table2.id
FROM table2
LEFT OUTER JOIN table1
ON table1.project_code = table2.id
WHERE table1.project_code IS NULL

Can I get data from a table based on multiple other tables?

$query = "SELECT * FROM table3 WHERE name_id = '(SELECT name_id FROM table2
WHERE salary < 1000 && name = '(SELECT name FROM table1
WHERE savings > 1000)')'";
Basically I want to get the data from the table1 based on the savings and use it to get the data from table 2 and use that data to get all the information from table 3. But this wont work. Is my code right or am I doing something wrong?
I also cannot create new tables, I simply want to display the data from table 3.
Use join
SELECT * FROM table3 t3 join table2 t2
on t3.name_id=t2.name_id
join table1 t1
on t3.name=t1.name
where salary < 1000 and savings > 1000
$query="SELECT * FROM table3 LEFT JOIN table2 ON table3.name_id=table2.name_id
LEFT JOIN table1 ON table3.name=table1.name
WHERE table2.salary < 1000 AND table1.savings > 1000 "
Another syntax of join is
SELECT * FROM table1 t1,table2 t2 ,table3 t3
where t1.name = t3.name and
t2.name_id = t3.name_id and
t1.savings > 1000 and t2.salary < 1000;
$query = "SELECT t3.* FROM table3 t3
INNER JOIN table2 t2 ON t2.name_id = t3.name_id AND t2.salary < 1000
INNER JOIN table1 t1 ON t1.name = t2.name AND t1.savings > 1000";

PHP SQL output issue adding a third table to a query

I am currently using this script below.
$query = mysql_query("SELECT * FROM `venues` as table1
LEFT JOIN `follows` as table2 on table1.venue_id = table2.venue_id
WHERE table2.user_id = $userid");
The tables have these fields:
Table1:
id, venue_id, user_id...
Table2:
id, venue_id, user_id...
The query above returns 5 records.
Now....
I need to add a third table to the above script Table3
Table 3 fields also contains id, venue_id, user_id... BUT I don't what it in the WHERE of the script.
I've tried adding a LEFT JOIN to the script above to add the third table like this:
$query = mysql_query("SELECT * FROM `venues` as table1
LEFT JOIN `follows` as table2 on table1.venue_id = table2.venue_id
LEFT JOIN `stats` as table3 on table1.venue_id = table3.venue_id
WHERE table2.user_id = $userid");
The Stats table only contains 1 record.
Now, my problem is that the query above it's echoing the data on ALL the records and not just the one.
My question is...What I'm I doing wrong on the line I added:
LEFT JOIN stats as table3 on table1.venue_id = table3.venue_id ?
OK, I think you want to also join on the user_id. So
SELECT
*
FROM
`venues` AS table1
LEFT JOIN `follows` AS table2 USING (venue_id)
LEFT JOIN `stats` AS table3 USING (venue_id, user_id)
WHERE
table2.user_id = $userid
is my solution
If you only want to include record from table1 that have non null records in table3 then you need to use INNER JOIN and not a LEFT JOIN. See the MySQL documentation for JOIN
$query = mysql_query("SELECT * FROM `venues` as table1
LEFT JOIN `follows` as table2 on table1.venue_id = table2.venue_id
INNER JOIN `stats` as table3 on table1.venue_id = table3.venue_id
WHERE table2.user_id = $userid");
The "INNER" is not needed explicitly. Joins are INNER joins by default
You're not limiting the records at all. Use this instead:
$query = mysql_query("SELECT * FROM `venues` as table1
LEFT JOIN `follows` as table2 on table1.venue_id = table2.venue_id
LEFT JOIN `stats` as table3 on table1.venue_id = table3.venue_id
WHERE table2.user_id = $userid AND table3.venue_id IS NOT NULL");
You can use an INNER JOIN rather than an LEFT JOIN. See this SO post to make it clear or this blog article

MySQL / PHP: Use inner join if value isn't 0

I want to use a select query which gets data from tables.
It looks like this
SELECT table1.value1, table1.value2, table1.id, table2.id, table2.value2
FROM table1
INNER JOIN table2
ON table2.id = table1.value4
ORDER BY table1.id DESC
LIMIT 10
Though table1.value4 can sometimes be 0 and there are no table2.id with the value 0 and since it's a auto-increment value, it starts at 1. And I want it to start at 1.
Because when it's equal to 0 then that specific row isn't available, just the other ones.
But I would like to some how set a where clause that it only should get the table2 values if table1.value4 isn't equal to 0.
What you actually want, it seems, is a LEFT JOIN then. All rows from table1 will be returned, even if there is no match in table2 (as with table1.value4 = 0).
SELECT table1.value1, table1.value2, table1.id, table2.id, table2.value2
FROM
table1
LEFT JOIN table2
ON table2.id = table1.value4
ORDER BY table1.id DESC
LIMIT 10
TRy this::
If you dont have 0 in table2 id, the below query works fine::
SELECT table1.value1, table1.value2, table1.id, table2.id, table2.value2
FROM table1
INNER JOIN table2
ON table2.id = table1.value4
ORDER BY table1.id DESC
LIMIT 10
But there are some values in table2 having id 0 and you want that to be ignored then
SELECT table1.value1, table1.value2, table1.id, table2.id, table2.value2
FROM table1
INNER JOIN table2
ON (table2.id = table1.value4 and table1.id!=0)
ORDER BY table1.id DESC
LIMIT 10

Categories