Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a database that looks similar to this:
ID | MESSAGE | TO | FROM | DATE
I need to query this database to check if NameA matches TO or NameB matches FROM. I also want to display these in order from newest to oldest (based on date).
mysqli_query($link, `SELECT * FROM [table] WHERE `to`='[NAMEA]' OR `from`='[NAMEB]' ORDER BY `date` DESC
That's the short answer. Sure, it can be improved, but this will depend on the structure of your table, the type of function you are using to access your database, etc.
Keep in mind: Do NOT use mysql_* functions. They are deprecated and may contain security vulnerabilities. Use mysqli_* or PDO. More documentation on these functions can be found at php.net.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
I have table zipcode which has a column called code. The Code column contains the prefix of zipcodes like
395
453
302
1203
12
I want to write an SQL query that checks if the provided zipcode matches one of the values in the table. (Something like isValid)
I want to avoid writing all zip codes in the database.
Valid Input zipcode
395004
395152
3952
1256
Can anyone help me? Thanks
simple sql query:
select *
from zipcode
where '395004' LIKE CONCAT(code,'%');
references: SQL - Query to find if a string contains part of the value in Column
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have this value in my mysql table called item.Item is a column name, I want to get the values present in between two date values using query. How to get that.
eg. i want from 2017-2-6 to 2017-02-07 values.
Please guide me. Its a string value
2017-02-06_CHAWAL,2017-02-07_RAJMA,2017-02-08_ROTI,2017-02-09_BENGAN MASALA,2017-02-10_DAAL
You need to have separate table column with date. So you need to split this 2017-02-06_CHAWAL to
| date | name |
| 2017-02-06 | chawal |
Then you, can use between:
SELECT * FROM table WHERE date BETWEEN '2017-02-05' AND '2017-02-07'
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm using this query :
select field from table1 where time LIKE '%2016-03%' order by time asc limit 1
My problem is that the table has thousands of rows and so it takes too long to search for 1 row. Is it normal?
LIKE queries are always going to be slower than looking for a specific value.
It'll help immensely to add an INDEX on the field and change your query to LIKE '2016-03%' (there won't ever be anything before the year in a timestamp so drop that first %). It'll be able to take a couple shortcuts, at least.
If you use LIKE and starts with % MySQL must make a FULL TABLE SCAN to find the correct Dates, but if you start direct with the year they can use a index : ... KIKE '2016-03-%'
Try adding an INDEX to your time column and as other have pointed out, remove the leading % from the query.
See this post for more info
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a table that have a CP value (numeric), for example, 28030, 28060, 27100 etc. And the user can introduce a number via PHP. I want to, having this number for example, 28050, order in MYSQL my table putting 28060 as the first position.
This is the basic of my table:
SELECT * FROM `tiendas` ORDER BY `CP`
ABS() will work. Here's a query that does the job:
SELECT
CP
FROM tiendas
ORDER BY ABS(CP- 28050) ASC
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Say I have a MySQL table like the one below with a list of names in an organisation, and IDs for each.
id name
1 John Doe
2 Richard Smith
3 Jane Market
... ...
Given user will query for the person's first and/or last name (with the possibility of typos and nicknames used) and php should return the closest match to their query.
For example, if a user enters "ricky" (nickname for Richard), it should return the ID for Richard Smith.
For nicknames, you have to create a separate MySQL table, with lookups (Many-to-One relationship).
For typos, you will have to loop all of your names and compare them to what the user has entered using the levenshtein function, or the similar_text function. The User Contributed Notes will help.