how to use REGEXP in sql query - php

I have a column in my table called to_user that the content of it like this:
id to_user
1. 1+2+3
2. 1
my query
select
* from notifications
where (type = 4 and to_user LIKE %+".$user_id."+%)
or (to_user REGEXP +?".$user_id."+? ) and status = 2
but it doesn't work .

you should enclose the strig for search in proper quotes (and remove + if is not part oth string of you sarch condition )
"select * from notifications where (type = 4 and to_user LIKE '%".$user_id."%')
or (to_user REGEXP '".$user_id."' ) and status = 2" ;
due the fact you are using string if you n need an exact macth is = istead of like and don't use regex too

$sql = "SELECT *
FROM notifications
WHERE (type = 4 and to_user
LIKE '%" . $user_id . "%')
OR (to_user REGEXP '\+" . $user_id . "\+' )
AND status = 2";

Related

Select first letter from firstname only

I have been battling with this for a while now can anyone offer a solution to this.
I want to only show results where the first letter of firstname is "blah".
$n=$_GET['n'];
("SELECT * FROM my_list
WHERE setid = '16-17'
and firstname like '$n%'
order by studentid asc")
The above is what is have so far but shows no results at all.
thanks all
Maybe use mysql's LEFT to get the first character.
SELECT
*
FROM my_list
WHERE
setid = '16-17'
AND LEFT(firstname, 1) = 'x'
ORDER BY
studentid ASC
Will return all firstnames starting with x.
$n = $_GET['n'];
$value = $n[0]; // will give first letter
("SELECT * FROM my_list WHERE setid = '16-17'and
firstname like '". $value ."%' order by studentid asc")

How to not exist on another table

How to not exist on another table on this two table?
Users table
User_relationships table
and this is my current query
"SELECT * FROM `users`
WHERE CONCAT(first_name, " ", last_name) LIKE '%' . $name . '%'
AND (`role_id` = 6 OR `role_id` = 4)
ORDER BY `first_name` asc limit 15"
and I want to add on query where guardian_id(user_id) not exist on the user_relationships table
UPDATE 1
select * from `users`
where not exists
(select 1 from `user_relationships`
inner join `users` on `user_relationships`.`guardian_id` = `users`.`id`
where `user_relationships`.`student_id` = 422)
I tried this and still returns me zero result.
I only have var name = ? and student_id = ?
try to use "NOT IN" query function with 'DISTINCT'.
following may help you.
select * from users where CONCAT(first_name, " ", last_name) LIKE '%' . $name . '%' and (role_id= 6 or role_id= 4) and (id NOT IN (select DISTINCT guardian_id from User_relationships where student_id = $student_id)) order by first_name ASC limit 15
Let me know if you still need some changes.

Select mySQL data with help of other table

I'm currently working on a message board which only displays data if values in another table are set.
My 2 tables look like this:
tbsubscribers:
-subID (int)
-msg1 (tinyint)
-msg2 (tinyint)
-msg3 (tinyint)
-email (varchar)
tbmessage
-id (int)
-name (varchar)
-mitteilung (varchar)
-sender (varchar)
-datum (datetime)
The actual messages are in the tbmessage table and to check which messages the user wants to see there is the tbsubscribers table.
Now, I want to display the messages. So if a user subscribed to msg1 and msg2 (so the value of both are 1) it should select all the data from tbmessage which has the name 'msg1' and 'msg2'.
I know how to work with select in mysql but don't know how I could select multiple tables, and especially in this case I'm kind of confused how I would do this.
Any help would be appreciated
A simple join should work:
SELECT * FROM `tbsubscribers` s
RIGHT JOIN `tbmessage` tb ON (
CASE WHEN msg1 = 1 THEN name = 'msg1' END OR
CASE WHEN msg2 = 1 THEN name = 'msg2' END OR
CASE WHEN msg3 = 1 THEN name = 'msg3' END
)
I created a sqlfiddle here, which should demonstrate the functionality you're looking for.
I'm going to make some edits to clarify how you should do this, using both the query above, and another query if need be.
There are a few ways you can do this. Way #1:
<?php
/*
* If you already have username (email) in session,
* then you can do this:
*/
$query = "SELECT m.* FROM `tbsubscribers` s
RIGHT JOIN `tbmessage` m ON (
CASE WHEN msg1 = 1 THEN name = 'msg1' END OR
CASE WHEN msg2 = 1 THEN name = 'msg2' END OR
CASE WHEN msg3 = 1 THEN name = 'msg3' END
)
WHERE email = " . my_escape_string_function($_SESSION['username']);
/*
* If tbsubscribers has a '1' in msg1, then all msg1's are grabbed.
* If tbsubscribers has a '2' in msg2, then all msg2's are grabbed...etc
*/
//Set $rows equal to the result
return $rows;
?>
Way #2:
<?php
/*
* If you already have username (email) in session,
* then you can do this:
*/
$query = "SELECT * FROM `tbsubscribers` s
WHERE email = " . my_escape_string_function($_SESSION['username']);
/*
* Run this query, and fetch the entire userrow, and put into $user
* An alternative could be to just store the entire user array into
* $_SESSION, as you wont have to query the database twice.
*/
$tofetch = array();
for ( $i=1; $i<=3; $i++ )
{
//Let's check msg1, msg2, msg3
if ( $user['msg' . $i] )
{
$tofetch[] = "name = 'msg" . $i . "'";
}
}
/*
* If tbsubscribers has a '1' in msg1, and a '1' in msg2 then the array
* will look like this:
* array(
* 0 => "name = 'msg1'"
* 1 => "name = 'msg2'"
* )
*/
//Throw an exception if all 'msg1', 'msg2', and 'msg3' are all 0 (Otherwise the query will fail)
if ( empty($tofetch) ) {
throw new Exception("You're not subscribed to any messages.");
}
/*
* Now it's a simple query. $tofetch will be imploded, to form a nice
* where statement. Using the example above, it will look like:
* name = 'msg1' OR name = 'msg2'
*/
$query = "SELECT *
FROM `tbmessage`
WHERE " . implode(' OR ', $tofetch);
//Set $rows equal to the result
return $rows;

Return row if non existent in other table

I have 2 tables
members
=======
id
f_name // get these values.
l_name
email
friends
=======
to
from
The users's I'd is the value $member_id, if it is present in "to" or "from" I want it to not return the other value of that row, so only non-friends are shown.
Im creating a page to allow members to search through the database to add their friends by email, or name.
I'd like to return all rows from members where there is no record of the userid in either the "to" or "from" columns of the friends table.
I know how to do most basic mysql but joins are an issue for me, i don't really understand how to write them, if anyone can help me out and maybe even explain it a bit that would be great!
My query now:
$query = "SELECT * FROM members WHERE f_name LIKE '%" . $word . "%' OR l_name LIKE '%" . $word . "%' OR mc_name LIKE '%" . $word . "%' OR email LIKE '%" . $word . "%' LIMIT 10";
//Where $word is the search term.
Modified Query
SELECT * FROM members WHERE id NOT IN (SELECT to FROM friends WHERE frm=$member_id) AND $member_id NOT IN (SELECT frm FROM friends WHERE to=$member_id) AND f_name LIKE '%" . $word . "%' OR l_name LIKE '%" . $word . "%' OR mc_name LIKE '%" . $word . "%' OR email LIKE '%" . $word . "%' LIMIT 10
Edited
So, suppose a user with id 3:
SELECT *
FROM members
WHERE id <> 3
AND id NOT IN (SELECT to FROM friends WHERE from = 3)
AND id NOT IN (SELECT from FROM friends WHERE to = 3)
[Other conditions....];
OR
SELECT *
FROM members
LEFT JOIN friends
ON (members.id = friends.to
OR members.id = friends.from)
AND (members.to = 3
OR members.from = 3)
WHERE friends.to IS NULL
AND id <> 3
[Other conditions....];
Maybe consider change the name of your "from" and "to" columns, to avoid confusion, because FROM and TO are reserved words.
Also, you can use the reserved word EXPLAIN , before the query, to see the difference of the number of rows being fetched.
You might be able to do that using an LEFT Join, which will return all rows from members even if there's no corresponding row on the friend's table.
Something like this:
SELECT a.*, f.* FROM members m LEFT JOIN friends f
A great article to understand the many joins there are is this one.

MySQL where column = 'x, y, z'

Here's my situation: I need to select all the messages where user_id = x OR y OR z.
I have an array in PHP:
users = ('1', '2', '3')
is there anyway to select all the messages where user_id = one of those values without having a massive query of:
user_id = '1' OR user_id = '2' OR user_id = '3?'
(I need to receive the messages of 100+ people so it would be inefficient)
Thanks
Use an IN clause.
SELECT *
FROM YourTable
WHERE user_id IN ('1','2','3')
Yes! You can use the IN operator:
user_id IN ('1', '2', '3')
If your array will always be safe and contain elements, you can do:
"user_id IN ('" . implode("', '", $users) . "')"
in PHP, too.
This is easy to do :
$query = "SELECT * FROM table_name WHERE user_id IN('1','2','3')";
Since your value is in array you can use:
$users = array('1', '2', '3');
$user_id = "'" . implode("', '", $users ) . "'";
$query = "SELECT * FROM table_name WHERE user_id IN($user_id)";
Hope this helps.
Probably you don't like IN keyword. Instead, you can use a regular expression like this:
select * from your_table where user_id regexp '1|2|3'
user_id >= 1 AND <= 3
Is one alternative.
before strings ids are:
$query = "SELECT * FROM table_name WHERE user_id IN('1','2','3')";
preformance int for ids:
$query = "SELECT * FROM table_name WHERE user_id IN(1,2,3)";

Categories