mysql using max as where condition - php

I got 4 fields
page_id
page_category_id
page_sequence
page_path
For example
record 1
page_id = 1
page_category_id = 22
page_sequence = 1
page_path = "1.jpg"
record 2
page_id = 1
page_category_id = 22
page_sequence = 2
page_path = "2.jpg"
record 3
page_id = 1
page_category_id = 23
page_sequence = 1
page_path = "23_1.jpg"
record 4
page_id = 1
page_category_id = 23
page_sequence = 2
page_path = "23_1.jpg"
record 5
page_id = 1
page_category_id = 23
page_sequence = 3
page_path = "23_1.jpg"
.. and more records
I want to do a draw statement that
$sql_select = "select page_category_id from my_table where max(page_sequence) < 3";
it will show all the page_category_id who max(page_sequence) is less than 3.
but I can't do it with max(page_sequence) as the where condition
How can I get it working.. Thanks!

SQL is such fun, since you can almost always solve things in several completely different ways. Here's a NOT EXISTS alternative:
select distinct page_category_id
from tablename t1
where not exists (select 1 from tablename t2
where t2.page_category_id = t1.page_category_id
and t2.page_sequence >= 3)
Return a page_category_id if there are no row with same page_category_id that has a page_sequence >= 3.

select page_category_id from my_table
GROUP BY page_category_id
HAVING max(page_sequence) < 2

Related

Looking for MySQL/MariaDB where query over muliple rows

Table Products
ad_id| property_id | property_value_id
69 4 1
69 7 6
69 6 3
67 7 6
...
For Example:
I need to search ((where property_id = 4 and property_value_id = 1) and (where property_id = 7 and property_value_id = 6))
Result should be: 69
The values im searching for are dynamic.
SELECT DISTINCT ad_id
FROM
(SELECT ad_id
FROM Producst
WHERE (property_id = 4 AND property_value_id = 1)
OR (property_id = 7 AND property_value_id = 6)
OR (property_id = 6 AND property_value_id = 3)
group by ad_id
having count(ad_id) = 3) as subquery
Should do the job.
You can join the table with itself three times, one for each pair of searched value. The query can take the form:
select distinct a.ad_id
from my_table a
join my_table b on b.ad_id = a.ad_id
join my_table c on c.ad_id = a.ad_id
where a.property_id = 4 and a.property_value_id = 1
or b.property_id = 7 and b.property_value_id = 6
or c.property_id = 6 and c.property_value_id = 3

MySQL SELECT where if condition group

Hi this is what I want to do:
to_id: 5
to_type: 1
from_id: 3
from_type: 2
I want to SELECT ALL WHERE to_id = 5 only if to_type = 1 AND I want to select * where from_id = 3 only if from_type = 2.
how can I do this in a single SELECT statement?
Looks like this is what you want
SELECT * FROM foobar WHERE (to_id = 5 AND to_type = 1) OR (from_id = 3 and from_type = 2)
That is, get all rows that either matches to_id = 5 and to_type = 1, or matches from_id = 3 and from_type = 2.

Fetch row with different data on same column

We have the table structure like
Fieldid userid data
41 2 1
12 2 0
41 3 1
12 3 1
We want the user which have (data=1 and filedid = 41) and (data = 0 and filedid=12)
We tried the query
select userid from table
where (data=1 and filedid = 41)
AND (data = 0 and filedid=12)
but it returns empty results.
Can anyone help how we will get the record using MySQL?
You can use grouping with a conditional aggregate in the HAVING clause:
SELECT userid
FROM mytable
GROUP BY userid
HAVING COUNT(CASE WHEN data = 1 AND FieldId = 1 THEN 1 END) >= 1 AND
COUNT(CASE WHEN data = 0 AND FieldId = 12 THEN 1 END) >= 1
you need to use union all like:
select * from table1 where Fieldid = 41 and data = 1 union all
select * from table1 where Fieldid = 12 and data = 0
attached image shows the output
Similar to #Giorgos, with multicolumn compare for conciseness:
select userid
from t
group by userid
having sum((data,fieldId) = (1, 41)) > 0
and sum((data,fieldId) = (0, 12)) > 0
When searching for results with one or another set of data you have to use OR keyword, not AND.
SELECT userid FROM table
WHERE (data = 1 AND filedid = 41)
OR (data = 0 AND filedid = 12)

Update table based on conditions from another table

I have 2 database tables
1- users
id points
1 100
2 3
3 1
2- user_pages
user_id lp_flag
1 0
2 0
3 0
I am trying to update lp_flag = 1 if any id points < 5 in users table.
Here is my code i want to run it using cron job only.
$Point_row = mysql_query("SELECT * FROM users WHERE points < 5 ");
foreach($Point_row as $val){if($val['points']<5) {
mysql_query("UPDATE user_pages SET lp_flag = '1' WHERE user_id = '$id'")
}else{
mysql_query("UPDATE user_pages SET lp_flag = '0' WHERE user_id = '$id'") }
}
}
expected results
user_pages
user_id lp_flag
1 0
2 1
3 1
becasue id's 2 & 3 points are <5 in users table.
You should do this in one query:
update user_pages up join
users u
on up.user_id = u.id
set up.lp_flag2 = (case when u.points < 5 then 2 else 0 end);
There is no need for a loop on the application side.
You can use this:
UPDATE user_pages SET lp_flag = 1
WHERE user_id IN (SELECT id FROM users WHERE points < 5)
If default value of lp_flag is '0' than no need to use ELSE condition for UPDATE '0'.

Delete query for two way friendship (mysql)

Friendship in my DB is a two way street. Check screenshot:
So user id 14 and 1 are friends.
I want a query that can be initiated by either party and that deletes two way street completely removing both rows.
How would I go about doing this? This is not simple WHERE asked asker = id.
Use AND and OR to create a condition from simpler conditions, like
(asker_user_id = 14 AND asked_user_id = 1) OR
(asked_user_id = 14 AND asker_user_id = 1)
DELETE FROM friendship
WHERE (asker_user_id = 14 AND asked_user_id = 1)
OR (asker_user_id = 1 AND asked_user_id = 14)
You could combine 2 IN conditions, to achieve this.
DELETE FROM foo WHERE asker_user_id IN (1, 14) AND asked_user_id IN (1, 14)
http://sqlfiddle.com/#!2/d1866/1
$slq = "SELECT * from Table where `id` = '13' "; //depending on user
$query = mysql_query($sql);
$result = mysql_fetch_array($query);
$asker = $result['asker_user_id']; //14 //
$asked = $result['asked_user_id']; //1
$id = $result['id'];
$del_sql = "DELETE from table WHERE asked_user_id = '.$asker.' AND asker_user_id = '.$asked.'";
$del_query = mysql_query($del_sql);
$del_first = "Delete from table Where `id` = $id";
$del_first_query = mysql_query($del_first);
UPDATED (and tested)
My approach works even if you don't know relationship between 1 and 14. If you are looking for really "(asker = 14 AND asked = 1) OR (asked = 14 AND asker = 1)", it is easy while you are thinking "This is not simple"
Here is what I have tested. I hope this help somebody (including OP). Thanks.
Given Data
CREATE TABLE tab
(
id int,
asker_user_id int,
asked_user_id int
);
insert into tab value(1, 2, 1);
insert into tab value(2, 15, 1);
insert into tab value(3, 14, 1);
insert into tab value(4, 1, 14);
insert into tab value(5, 5, 1);
insert into tab value(6, 1, 5);
insert into tab value(7, 2, 5);
Q1 - find all relationship
SELECT t1.asker_user_id, t1.asked_user_id
FROM tab AS t1 JOIN tab AS t2
WHERE t1.asked_user_id = t2.asker_user_id
AND t2.asked_user_id = t1.asker_user_id
+---------------+---------------+
| asker_user_id | asked_user_id |
+---------------+---------------+
| 1 | 14 |
| 14 | 1 |
| 1 | 5 |
| 5 | 1 |
+---------------+---------------+
Q2 - before delete, check relationship's record id
SELECT DISTINCT A.id as record_id
FROM tab A JOIN
(
// insert Q1 here
) B ON (A.asker_user_id = B.asker_user_id AND A.asked_user_id = B.asked_user_id)
OR (A.asked_user_id = B.asker_user_id AND A.asker_user_id = B.asked_user_id)
+-----------+
| record_id |
+-----------+
| 3 |
| 4 |
| 5 |
| 6 |
+-----------+
Q3 DELETE all friendship record
DELETE A
FROM tab A JOIN
(
// insert Q1 here
) B ON (A.asker_user_id = B.asker_user_id AND A.asked_user_id = B.asked_user_id)
OR (A.asked_user_id = B.asker_user_id AND A.asker_user_id = B.asked_user_id)
Query OK, 4 rows affected (0.00 sec) <== records which has friendship are deleted.
Orig. Answer
Not tested, but I guess it will display both 13 and 14
SELECT *
FROM tab A JOIN
(
SELECT t1.asker_user_id, t2.asked_user_id
FROM tab AS t1 JOIN tab AS t2
WHERE t1.asked_user_id = t2.asker_user_id
AND t2.asked_user_id = t1.asker_user_id
) B ON (A.asker_user_id = B.asker_user_id AND A.asked_user_id = B.asked_user_id)
OR A.asked_user_id = B.asker_user_id AND A.asker_user_id = B.asked_user_id)
when above query works, then delete like this:
DELETE FROM tab
WHERE id IN (
SELECT A.id
FROM tab A JOIN
(
SELECT t1.asker_user_id, t2.asked_user_id
FROM tab AS t1 JOIN tab AS t2
WHERE t1.asked_user_id = t2.asker_user_id
AND t2.asked_user_id = t1.asker_user_id
) B ON (A.asker_user_id = B.asker_user_id AND A.asked_user_id = B.asked_user_id)
OR A.asked_user_id = B.asker_user_id AND A.asker_user_id = B.asked_user_id)
)
Selfjoin:
SELECT *
FROM friendship asker
LEFT JOIN friendship asked
ON asker.asked_user_id = asked.asker_user_id
WHERE asker.asker_user_id IS NOT NULL
I cannot test right now (or just lazy to create an sqlfiddle),
but it is something like this.
I maybe switched asked and asker in a way.
This is the way you can test if you have both (instead of select, use delete, after checking the correct fields)

Categories