Select from all tables with same start - php

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

Related

Find table name & then get data from that table.

Model
ID Model
1 1
2 2
3 4
4 10
Model-table
ID model_id table_name
1 1 table1
2 3 table2
3 4 table3
Note: It is not important that every model requires table_name.
I find the table_name of model width id 3, it is table2. It is so simple fo find it, I do not see the reason to write here my sql. After finding table2, I should select from table2 and find all parametres. In this case I should write second sql. Here is the structure of table2
Note: We can find all parametres of model_id with 3.
table2
ID model_id param1 param2 param3
1 3 0 5 10
My question:
I am looking for the way, firstly find suitable model_id in model_table and fetch table_name, THEN SELECT from table_name and fetch all parametres with 1 SQL.
Note: I have id and model_id variables on my PHP PAGE. So, we need to use Limit 1 and fetch suitable data to 1 id and model_id.
You have to do this in two queries as table names cannot be dynamic. Your code should look something like this:
$mysqli = new MySQLi(/*connection details*/);
// Fetch table name
$query = "SELECT table_name
FROM model-table
WHERE model_id = 3";
$result = $mysqli->query($query);
$row = $result->fetch_assoc($result);
$table = $row['table_name'];
// Fetch data from table2
$query = "SELECT ID, model_id, param1, param2, param3
FROM $table";
$result = $mysqli->query($query);
while($row = $mysqli->fetch_assoc($result)) {
// do something with the data from the table...
}
Not really what you are looking for, but this can help you too
Find all the tables of a database
SHOW TABLES
Find all the columns of a table
DESCRIBE table_name

Selecting the next results in MySQL

How can I select the next result just by specifying the name?
I have a table that looks like this, no IDs, just list of usernames.
row1 = username(Alex)
row2 = username(Bob)
row3 = username(Britney)
row4 = username(Steve)
row5 = username(Courtney)
row6 = username(Greg)
row7 = username(Abul)
row8 = username(Roger)
row9 = username(Victoria)
row10 = username(Brooke)
Let's say, I want to select all Items after 'Greg'. How can I achieve this?
No, there is no inherent ordering of tables in SQL, so there is no way (other than by alphabetical order) to determine which name comes next.
In fact, the order displayed in the question is entirely arbitrary - without a specified order by clause, retrieval order of rows is essentially random.
this can be done by two query..first find the id of the particular name.
like select id from tablename where username='Greg'
then fetching that id,
select * from tablename where id>fetching id.
Add ID column to your table, and then use LIMIT as offset
SELECT * FROM users LIMIT (SELECT ID FROM username WHERE username='Greg'), MAX(ID)
Didn't tested, but you can play with it

Checking for string match with a MySQL query

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.

mysql with the date function

I have a mysql table containing a field name dtt_date and have values like
08/04/2010 22:15:00. I want to display all the records with in this month (08, august), How to write a mysql query in my php page to display these record.
Does any one know this?
Please help me?
SELECT * FROM table WHERE dtt_date>='2010-08-01' AND dtt_date<='2010-08-31';
In PHP:
$q = "SELECT * FROM table WHERE dtt_date>='2010-08-01' AND dtt_date<='2010-08-31'";
$res = mysql_query($q);
while($row = mysql_fetch_assoc($res))
var_dump($row);
mysql_free_results($res);
Untested, I'm sure there are easier methods to this. Not sure if your date format will be handeled by MySQL
SELECT * FROM table WHERE MONTH(DATE_FORMAT(date,'%Y-%m-%d')) = 8
You can try this one method
SELECT * FROM table WHERE month(dtt_date)='08' AND year=(dtt_date)='2010';
You should change the date format in the table to be '2010-08-04 22:15:00', then you could run this query:
SELECT DATE_FORMAT(dtt_date, '%D %M %Y') as date FROM myTable
From there you would get something of this as a result, and then you can experiment with the date formatting.
+-----------------+
| date |
+-----------------+
| 4th August 2010 |
+-----------------+

Sql results into php array

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 ;

Categories