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.
Related
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
I have a query
SELECT *
FROM `user`
WHERE NOT
EXISTS (
SELECT *
FROM roleInEvent
WHERE user.userId = roleInEvent.userId
AND eventId = 1
)
AND user.disciplineId =5
Whenever I run this in my mysql console it returns 1 row. This is correct.
However in my php script it returns zero rows while it is exact the same query.
My php script looks like this:
$db = DatabaseHelper::get();
$st = $db->prepare('SELECT *
FROM `user`
WHERE NOT
EXISTS (
SELECT *
FROM roleInEvent
WHERE user.userId = roleInEvent.userId
AND eventId = 1
)
AND user.disciplineId =5');
$st->execute();
if ($st->errorCode() !== \PDO::ERR_NONE) {
return null;
}
Somebody knows what is the problem here?
The solution was easy.
I forgot to return the results to my javascript
You can use too the IN with a NOT like:
SELECT * FROM user WHERE id_user NOT IN ( SELECT other_data FROM roleInEvent WHERE user.userId = roleInEvent.userId AND eventId = 1 ) AND user.disciplineId =5
If the query: SELECT other_data FROM roleInEvent WHERE user.userId = roleInEvent.userId AND eventId = 1 bring to you something, this result not have to exist in the user table.
Other example is:
Users Table:
---------------------------
id_user | name_user
1 fernando
2 urban
And in other table we have the block users:
Block Users:
id_user_block | id_user | name_user
1 1 fernando
2 2 john lennon
So,i want bring to me only users that not exists in the block users table:
The query will be:
SELECT * FROM Users WHERE id_user NOT IN ( SELECT id_user FROM blocked_users )
So, the last query we are getting:
id_user | name_user
2 john lennon
Greetings!
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
This is how my table looks like:
id | name | value
-----------------
1 | user1| 1
2 | user2| 1
3 | user3| 3
4 | user4| 8
5 | user5| 6
6 | user7| 4
7 | user8| 9
8 | user9| 2
What I want to do is to select all the other users, in one query, who's value is user1's value lower than it's value plus 3, higher than it's value minus 3 or equal to it's value.
Something like this:
$result = mysqli_query($con,"SELECT * FROM users WHERE value<'4' OR value>'-2'") or die("Error: ".mysqli_error($con));
while($row = mysqli_fetch_array($result))
{
echo $row['name'].'<br/>';
}
The problem is that users1's value can vary every time the query is run.
Sorry for lame names, but this should work:
NOTE: I named table with your data as "st".
SELECT b.user, a.value as "user1val", b.value as "otheruservalue" FROM st as a
join st as b
on a.user = "user1" and a.user != b.user
where
(b.value > (a.value - 3)) and (b.value < (a.value + 3))
We get unique pairs of user1's value and other user's value by joining same table. After that we just do some simple comparison to filter rows with suitable values.
$user1 = mysql_fetch_assoc(mysql_query("SELECT `value` FROM `users` WHERE id='1'"));
$result = mysql_query("SELECT * FROM `users` WHERE value<'".$user1['value']."+3' OR value>'".$user1['value']."-3'");
Or nested queries :
$result = mysqli_query($con, "select * from `users` where `value` < (select `value` from `users` where `name`='user1')+3 OR `value` > (select `value` from `users` where `name`='user1')-3");
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)