I am trying to all grab rows of data from a data table, where one word matches a word within the text in my column 'story'.
Table Structure:
post_id | user_id | story
----------------------------------
1 | 1 | Hello World What's up?
2 | 4 | Hello guys!
3 | 7 | Working on shareit.me!
For Example:
I want to grab all of the posts containing the word hello (I am looking for case-insensitive).
How can I do this?
Here is what I have done so far:
// this will be the keyword! so use the variable $filter_tag in the query!
$filter_tag= $_GET['tag'];
//query for getting the users' posts containing the select tag
$query_posts= "SELECT * FROM posts WHERE user_id= '$user_id' ORDER BY post_id
DESC";
$result_posts= mysqli_query($connect, $query_posts);
Thanks!
$query_posts= "SELECT * FROM posts WHERE user_id= '$user_id' AND story LIKE '%$filter_tag%' ORDER BY post_id
DESC";
SELECT * FROM posts WHERE ... AND LOWER(story) LIKE '%hello%'
OR
SELECT * FROM posts WHERE ...
AND story COLLATE latin1_general_ci_ai LIKE '%hello%'
It would be:
SELECT * FROM posts WHERE ... AND story LIKE '%hello%'.
Generally the "%" is a wildcard (like '*'). So you can use 'hello%' '%hello%' and so on.
You can use the LIKE operator:
$query_posts = "SELECT * FROM posts WHERE user_id = $user_id AND story LIKE '%yourword%' ORDER BY post_id";
The % characters are like wildcards. % means match any number of characters, where _ (underscore) matches just one.
Note: I've removed the single quotes from your user_id column check too - this, being an integer, doesn't want to be in quotes - they are for strings.
Goes without say don't forget to escape the input.
$query_posts= "SELECT * FROM posts WHERE user_id = '$user_id' AND story LIKE '%".mysql_real_escape_string($filter_tag)."%'";
My answer is like the same, i made a sqlfiddle:
#supose the tag is Hello
SELECT * FROM posts
where story like "%Hello%";
PS: http://sqlfiddle.com/#!2/6bfcc1/5
The best solution will be to use MATCH() AGAINST() with an FULLTEXT index.
Related
This is my table:
ID NAME GROUP
123456 Example 1
789012 Test 2
345678 Lorem 1
This code works fine:
select * from mytable where id="789012"
However, this code fails:
select * from mytable where group="1"
Why is this? Isn't the whole point of iterating with the while loop to return multiple rows?
Your query fails. You need to escape reserved words in MySQL like group with backticks
select * from mytable where `group` = 1
I have a MySql database with some rows as follows:
ID DESC
1 This is my bike
2 Motorbikes are great
3 All bikers should wear helmets
4 A bike is great for exercise
5 A. Top. Bike.
What I want to do is return the rows with whitespace surrounding the search term, OR the term being at the end or beginning of the description.
For example,
"SELECT * FROM `mytable` WHERE `desc` LIKE '%bike%'"
Will return all rows. But,
"SELECT * FROM `mytable` WHERE `desc` LIKE '% bike %'
Will only return row 4.
What I really want is a reliable way to return rows 1, 4 and 5, i.e. where the search term is sorrounded with anything BUT chars A-z, 0-9. Any ideas? Is this even possible with MySql?
Thanks!!
You can use regular expressions in SQL
SELECT * FROM `table` WHERE desc REGEXP '\bbike\b'
You should start reading about MySql RegEx.
Sample Code.
SELECT * FROM table WHERE field_name REGEXP PATTERN;
More Specific
details Table
ID NAME
1 Dipesh
2 Dip
3 Dipe
4 DiDi
5 Di
SELECT * FROM details WHERE NAME REGEXP '^Di$';
Result
NAME -> Di
SELECT * FROM details WHERE NAME REGEXP 'Di$';
Result
NAME -> DiDi , Di
SELECT * FROM details WHERE NAME REGEXP '^Di';
Result
NAME -> Dip, DiDi, Di
You need to specify the additional conditions in the query:
SELECT *
FROM `mytable`
WHERE
`desc` LIKE '% bike %' OR
`desc` LIKE '% bike' OR
`desc` LIKE 'bike %';
Try this one, hope it'll help you
"SELECT * FROM `mytable` WHERE `desc` LIKE '% bike'
How to select all tables who whas fs_ for example, its not a prefix, its a something added from me , but some tables has it, and i want to select all of them, not choasing the one by one, but to select all tables who starts with fs_
in your MySQL database you can create an extra column that will contain such value.
ex:
ID | NAME | DATE | FS_COLUMN
1 jon 2011 1
2 doe 2005 0
3 tom 2001 1
Then with PHP you can fetch these rows.
$q = mysql_query("SELECT * FROM table WHERE FS_COLUMN='1'");
$r = mysql_fetch_array($q);
$q = $dbc -> ("SELECT COUNT (*) fs_");
$q -> execute();
$count = $q -> rowCount();
echo $count;
It is hard to tell what you are asking or what you are trying to achieve!?
From what you have said try that, if not then please elaborate your question in a more clear and concise way.
well ... it's really no good design approach but to answer your question:
you will probably want to get all matching tables by
SELECT table_name
FROM information_schema.tables
WHERE table_schema = '___YOUR_DB_NAME___'
AND table_name LIKE 'fa\_%'
and construct your query based on the result
I'm sure this is super easy, but can't seem to figure it out.. I need to select all titles from my database where the title starts with A, or B, or C etc. Here's what I've tried so far:
SELECT * FROM weblinks WHERE catid = 4 AND title LIKE 'A'
but returns nothing.. Could someone help me out with this?
Cheers
For titles starting in 'A' use a % after the A
SELECT * FROM weblinks WHERE catid = 4 AND title LIKE 'A%'
For titles with the letter 'A' in it, use % on either side of A
SELECT * FROM weblinks WHERE catid = 4 AND title LIKE '%A%'
For titles ending in the letter 'A', use % before the A
SELECT * FROM weblinks WHERE catid = 4 AND title LIKE '%A'
Basically % is a wildcard. It tells MySQL that anything can be in the location.
For having numbers as the first letter, check out Mark's answer.
The wildcards for LIKE are % and _, where % matches 0 or more characters and _ matches exactly one character.
The existing answers are correct for beginning with A:
SELECT * FROM weblinks WHERE catid = 4 AND title LIKE 'A%'
For beginning with any number you can use the REGEXP operator:
SELECT * FROM weblinks WHERE catid = 4 AND title REGEXP '^[0-9]'
try:
SELECT * FROM weblinks WHERE catid = 4 AND ((title like 'A%') OR (title like 'B%'))
so on and so forth
SELECT * FROM weblinks WHERE catid = 4 AND title LIKE 'A%'
% tells "anything", so it's "A" then anything. Only works with the LIKE comparison operator.
I would like to create an array (in php) from sql results like this:
We have the sql-table "Posts" which stores the Name and the Message.Example:
Name | Message
John | Hello
Nick | nice day
George | Good bye
John | where
What i want is to output the names of people who have posted a message but dont display the same names more than 1 time.
So the output would be John,Nick,George.
(From these records, we see that John has posted 2 messages, but at the final output, we see only one time his name).
Is this somehow possible?
Thanks in advance.
Try:
$sql = <<<END
SELECT DISTINCT Name FROM Posts
END;
$query = mysql_query($sql) or die($sql . ' - ' . mysql_error());
$names = array();
while ($row = mysql_fetch_array($query)) {
$names[] = $row[0];
}
print_r($names);
SELECT DISTINCT
You could run a SQL query to just select the distinct names, and nothing else:
SELECT DISTINCT Name FROM Posts;
This will give you a result set consisting of distinct Names values, with each unique value only being returned 1 time in the set.
to get the count you will need to aggregate using group by:
SELECT
NAME
, COUNT(*) as Posts
FROM
Posts
GROUP BY
NAME
Here is the SQL if you are not averse to group BY
select count(name) as N, name from posts group by name ;
People having more than 1 post
select count(name) as N, name from posts group by name having N > 1 ;