Optimize 2 mysql queries into one - mysql

Using php and mysql 5.x. I currently load a banner image in a certain section of my site like so:
SELECT * FROM banners WHERE section = 1 AND pageid = 2
But if no results found I run a second query:
SELECT * FROM banners WHERE section = 1 AND pageid = 0
Basically what Im doing is trying to find banner images assigned to that section for that page. If no results found then I look for any default banner images in the second query. Is there a better way where I can do this in one query?
EDIT
To clarify a little bit more. I want to check if there is any banners assigned to the page, if not then see if there is any banners assigned to 0 (Default). I dont want a mix of both either it shows all banners assigned to that page or show all banners assigned to pageid 0 and there could be a possibility of multiple rows returned not just one.
ADDITIONAL EDIT
To better explain what this is for. In an admin tool I allow someone to assign a banner image to section on the website. In the admin tool they can select the section and the page they want the banner image to show. They can also set the default banner image(s) for that section. So if there were no banner images assigned to a section by default it will load the banner image(s) assigned to 0 for that section throughout the website. So instead of assigning a default banner image to 50 different pages they can just do it one time and it will load the default banner image or images for that section. Just trying to find a way to do this in a more optimal way, instead of 2 queries could it be done in one?

The OR operator will make the conditional (pageid = 2 OR pageid = 0) return true immediately if just the first value is true and since there is a LIMIT 1 I think it should always fetch one with pageid = 2 first since the order of pageid is DESC and 2 is bigger than 0.
SELECT * FROM banners WHERE section = 1 AND (pageid = 2 OR pageid = 0) ORDER BY pageid DESC LIMIT 1
EDIT: I'm not positive if the ORDER BY is necessary, I'd love to see some comments on that

Maybe it's not the most elegant way, but you can try this:
SELECT *
FROM banners
WHERE section = 1
AND pageid = IF(
(select count(*) from banners where section = 1 and pageid = 2) = 0,
0, 2
);

Related

Page numbering by group on report

I have a report where I want to show records by group in different pages.
Assume that groups have enough records to cover more than one page . In addition in the group footer section I have totals.
I want page numbering by group . For example group 1 covers 2 pages so the numbering must be Page 1 and Page 2 , group 2 covers 3 pages so the Page number must be Page 1 ,Page 2 and Page 3.
I have succeeded on numbering by group but when the page shows only the group footer section where I keep the totals the page numbering resets for example if Page 3 of Group 2(previous example) shows only the totals instead of Page 3 shows Page 1. The code I use is :
Private sub GroupHeader
Page =1
End sub
Private sub GroupFooter
Page = 0
End sub

Limit 1 page display only 5 data using yii

I have a problem regarding setting limit for specific page. For example i have 8 data. In page 1, only 5 data are allowed to display. the remaining data will display at the next page. How to make this possible to if the total data i get is from database. How to make this condition in sql??
Is not clear what you are asking
but you can limite the row showd in gridView with $dataProvider->pagination->pageSize
eg:
$dataProvider->pagination->pageSize=5;
thank you for the response. i already found an answer..:)
//$i refer to total of page.
$start_from = ($i-1)*5;
$RsPermitSkvLim = RsPermitSkv::model()->findAllBySql("SELECT * FROM rs_permit_skv WHERE id_permit_info = $id_permit_info ORDER BY id ASC LIMIT $start_from,5");
foreach($RsPermitSkvLim as $rsskvinfo) {
//coding here.
}

Comparing two url-slugs to find count of same words

I want to find similar posts on my website depending on the url slug.
say i have the following five slugs
i-am-a-slug /*the slug i want to compare*/
i-am-another-slug /* 3 same words */
i-am-an-ant /* 2 same words */
the-slug-life /* 1 same word */
foo-bar /* 0 same words */
at the moment i am using the following code to find out if there are any similar words in the compared slug
SELECT *
FROM News
WHERE News.slug != "i-am-a-slug"
ORDER BY CASE
WHEN News.slug REGEXP "i|am|a|slug" THEN 1
ELSE 2
it doesn't even work very well... words like the a in the example give back hits in nearly every slug i have in my database... in the example, even the slug foo-bar would be returned.
i can't seem to figure out how to select a variable same-words-count that counts all the same words within each tested slug (see comments first code-block for the solutions i would like to get), so i could
SELECT *
FROM News
WHERE News.slug != "i-am-a-slug"
ORDER BY CASE
WHEN same-words-count > 2 THEN 1
WHEN same-words-count = 2 THEN 2
WHEN same-words-count = 1 THEN 3
ELSE 4
or is there an even better way to do this?
thank you very much in advance, sorry my mysql is a bit rusty lately...
What you are looking for is called inverted index: http://en.wikipedia.org/wiki/Inverted_index
Depending on the data amount and what you actually are going to do with the result (do you need to keep it up to date, do you need to show it on the site, etc) you might want to solve the problem using a programming language of your choice or use some full-blown fulltext search solution. Plain SQL just isn't a right tool for this.

SSRS - Column Group - Table Horizontally Expanded

There was a requirement, to have data horizontally expanded in the Rows. So I have created Child Row group in Column group of Matrix as shown in this link.
Reference Link
It is working fine. And it displays result as below.
Requirement was : Show Thumbnails of images uploaded for a building as above.
But now the problem is, when there will be number of images, this is going to be expanded horizontally.
I want to repeat this Row after 8 or 10 images.
Any Idea how can I achieve this in SSRS ?
Thank you,
Mittal.
Not quite sure about your requirement, do you want those images wrap in your report, each row has maximum 8 images? If so, we need to make each 8 images into one group. In this scenario, we can create a list. If you have a index field (like a specific id for each image) in your dataset, we can put in the group expression with this:
=ceiling(Fields!Index.Value/8)
If you don't have this kind of index column, we can make it manually. Embed the custom code below:
Dim CountNumber As Integer = 0
Public Shared Previous as Object
Public Function GroupNumber(ByVal category As Object) As Integer
If Category <> Previous then
CountNumber = CountNumber + 1
Return CountNumber
Else
Return CountNumber
End If
End Function
Then replace the group expression with this:
=ceiling(Code.GroupNumber(Fields!Image.Value)/8)
I have tested in my local environment and it works. But I can't share the screenshot due to low reputation.

To fetch Random/different records each time

I have 3 thumbnails which i want to rotate each time a user refresh the page. Therefore want to show different thumbnail each time. I have image name stored in the database. I am using this SQL query to fetch "select thumbnail from tablename order by rand() limit 1". It is fetching one thumbnail perfectly.
The problem is that sometime same images comes up. For example first time i refresh the page thumbnail 1.jpg shows but second time again 1.jpg comes up.
So what i am looking for is to have different thumbnail each time i refresh the page.
Can anyone provide me some suggestions to achieve this ?
this is not possible with random selection, as the same image can come again.
For the effect you want you need to use a cookie to store the last displayed image name and NOT display that image for the next request.
As you have confirmed in your comment you do not want to display the last displayed thumbnail. IN that case you can do this like this using sessions:
// get last shown from session if any
$last_shown = '';
if(isset($_SESSION['last_shown_thumb']))
$last_shown = $_SESSION['last_shown_thumb'];
// select a thumbnail randomly which is not the one last shown
$query = "SELECT thumbnail FROM tablename WHERE thumbnail <> '" . $last_shown . "' order by rand() limit 1";
$res = mysql_query($query);
$row = mysql_fetch_row($res);
$curr_thumb = $row[0];
// set the current to last shown in session
$_SESSION['last_shown_thumb'] = $curr_thumb;
Assuming you are using PHP, you store the previous thumbnail in $current_thumbnail.
Then, you can do it like this
SELECT thumbnail FROM tablename WHERE thumbnail <> '$current_thumbnail' ORDER BY rand() LIMIT 1
Your query just needs to know what thumbnail you are currently displaying so you can filter it out.