MYSQL- Query to search using 'like' but searching only specific columns [closed] - php

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'm using this query to select data:
mysql_query("select * from tenders where Heading LIKE '%{$key}%' OR Date LIKE '%{$key}%'");
now there is a field status which indicates whether tender is online or offline i.e shown on website or hidden.
I want to select only those tenders whose status is online.
Can anyone help me with this? I am unable to use multiple where clause where status=online
How can I write query for this?

You don't write multiple WHERE clauses, you write multiple conditions in the WHERE clause. (Exactly like your query already does with the two LIKE comparisons, though perhaps with some parentheses to specify the logic a little more explicitly.) Something like this:
SELECT
*
FROM
tenders
WHERE
(Heading LIKE '%{$key}%' OR Date LIKE '%{$key}%')
AND status = 'online'
Also, I think you mean that status is a column, not a row. Otherwise, this whole thing becomes much more confusing...

mysql_query("SELECT * FROM tenders WHERE Heading = '%{$key}%' OR Date = '%{$key}%'");
Using WHERE x = y OR z = a should work in your case, I am a little confused on exactly what you want but this should help!

Related

How to order two tables with no relationship and different columns, by a single column that they share (date) [closed]

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 4 years ago.
Improve this question
I've got two tables in a MYSQL database. One Is a for blog posts and one is for videos. All the columns in each of them are different except for one which is the date they were created.
On the main page of my website I want to show most recent posts, whether that be a video or a blog post. So I want to order them based on their shared date column. Is this something I can do in MYSQL or would I have to pull all the data into php and then order it using my own function.
I've looked up other answers but they all seem to be cases where the tables have no relationship but share the same columns.
For table to be used in the same result set as you are suggesting, you would need a UNION; but UNIONs require all union-ed queries to have the same columns in their results. If the only field in common you have is "date"; the best you could probably do is something like.
SELECT `date` AS postDate, 'Video' AS postType
, someVideoField, null as someBlogField
FROM video_table
UNION
SELECT `date` AS postDate, 'Blog' AS postType
, null as someVideoField, someBlogField
FROM blog_table
ORDER BY postDate
;
Note: The latter aliases are not actually needed, I just tend to do that for clarity as to which field is expected to map to which. Also, the null as someBlogField portion may need tweaked to insure the result field is a type that can accept the real values from the latter half of the union; the first half determines field types.
If you end up using two queries it's easy enough to put them together in your script.
while ($row = $blogQuery->fetch()) {
$results[$row['date']]['blog'] = $row;
}
while ($row = $videoQuery->fetch()) {
$results[$row['date']]['video'] = $row;
}
If you ordered by date in your select queries, this will already be mostly in the right order, except for dates where only the video query has rows (or whichever one you fetched second). You can ksort($results) to fix the order for any of those.

Random like order with mysql and PHP [closed]

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 use PHP and Mysql. I have a table that looks kind of like this:
id title
----------
1 my title
2 another title
3 The last title
Now I want to select them with a random like order.
I will need to use LIMIT because of the query size.
The random like order should always be the same random order every time.
Example result, every time
3 The last title
1 my title
2 another title
Do another query run:
3 The last title
1 my title
2 another title
The same random like result appear.
Possible solutions
Add a real random number stored as a new column generated by insert.
Some fancy SELECT query that does some magic.
Something else?
Why I want this is that I insert products, first from one site, then from another. In the result I want to present them as a mix.
It's not really random at all, but then again your request wouldn't work with random numbers. You want some sort of a hash of each record to use in the order by. You can probably find something better, but as a simple example you could use:
SELECT * FROM my_table ORDER BY MOD(id, 2);
Which you can see working here:
http://sqlfiddle.com/#!9/269a4/1/0
Use ORDER BY RAND().So your query should be :
select * from <tablename> where <cond> then ORDER BY RAND() limit <startlimit>, <end limit>

MySQL Join Users table to Groups table, find users not in any of selected groups [closed]

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
How do I write a query that takes a list of group ids (could be 1, could be 10, etc) and gets the users who are not in ANY of those groups?
We are using Doctrine, but we can't even figure out how to do it in raw SQL.
It's okay to have PHP generate part of the query if we need to do multiple joins/conditions etc based on how many ids are provided.
If my general assumptions about your table structure are correct, I believe something like this should work:
SELECT *
FROM users
WHERE user_id NOT IN (
SELECT DISTINCT user_id
FROM user_groups
WHERE group_id IN ([your group list])
);

Mysql pattern recognition and update [closed]

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've got a mysql table - that contains a column of links
like www.example.com/?book=2
how do I select through the entire table - but extract the id, to put it into its own column? A kind of select, update query?
Would it be easier to just copy the column - then force it to an int type? Would that then leave me the id as a number?
What is the best solution for this.
SELECT id, sourcelink FROM books
UPDATE books SET id=1 WHERE sourcelink ='www.example.com?book=1';
You could do:
update books
set id = substring_index(sourcelink, '=', -1) + 0
where sourcelink like '%\?%=%';
This sets the id to whatever is after the = converted to a number. The where clause checks that the sourcelink has a structure similar to what you expect. This is a rather loose pattern. It could be made more specific by using regular expressions.
This is a mischievous but correct answer, if your id number always comes at the end of the text string.
UPDATE books
SET id = CAST(REVERSE(CAST(REVERSE(sourcelink) AS INT)) AS INT) AS id
WHERE CAST(REVERSE(CAST(REVERSE(sourcelink) AS INT)) AS INT) <> 0
This relies on the quirk of MySQL's casting of a string to an integer that causes it to understand the string value 99 red balloons as the integer value 99.

How to echo fetched array data from a mysql database in custom order? [closed]

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
I have been trying to print out data from a database table in custom order like for example..
I have a table and that have alot rows and have one column as listing_type which have values like Gold,Premium,Silver,Free etc for each row..! so how I would be able to print the data from fetched array in order like at first it should echo all Gold and then Premium and then Silver and then Free etc like..!
Any help would be appreciated..Thanks waiting for your reply.!
Use an ORDER BY clause in your MySQL query like this:
SELECT *
FROM `table`
ORDER BY FIELD(`listing_type`, 'Gold', 'Premium', 'Silver', 'Free')
The MySQL function FIELD() returns the position of str in the given strings. With this, you can create a custom order to sort your results on.
In Mysql query you can easily order results by using field() function,so returned results will be ordered in way where listing_type is gold first then premium then silver results then free
select *
from your_table
order by field(listing_type,'Gold','premium','Silver','Free' )

Categories