Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8199051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:05:34+00:00 2026-06-07T06:05:34+00:00

I’ve a query that I need some help with – As part of a

  • 0

I’ve a query that I need some help with –
As part of a form I’ve got a serial number field that is populated if there is a serial number, blank if it’s not, or no result if it’s an invalid serial number.

select * 
from cust_site_contract as cs 
where cs.serial_no = 'C20050' or (cs.serial_no <> 'C20050' and if(cs.serial_no = 'C20050',1,0)=0)
limit 10;

Here’s a sample of the regular data:

+----------------------+-----------+-----------+-----------
| idcust_site_contract | system_id | serial_no | end_date
+----------------------+-----------+-----------+-----------
|               561315 | SH001626  | C19244    | 2009-12-21
|               561316 | SH001626  | C19244    | 2010-06-30
|               561317 | SH002125  | C19671    | 2010-05-31
|               561318 | SH001766  | C14781    | 2010-09-25
|               561319 | SH001766  | C14781    | 2011-02-15
|               561320 | SH002059  | C19020    | 2008-07-09
|               561321 | SH002639  | C18889    | 2008-03-31
|               561322 | SH002639  | C18889    | 2008-06-30
|               561323 | SH002715  | C20051    | 2010-04-30
|               561324 | SH002719  | C20057    | 2010-04-30

And an exact result would look something like this:

|               561487 | SH002837  | C20050    | 2012-07-04

I was writing this as a subquery so I could match the system_ids to customer and contract names, but realised I was getting garbage pretty early on.
I’m tempted to try and simplify it by saying the third case might not hold true (i.e. if it’s an invalid serial number, allow the choice of any customer name and simply flag it in the data)
Has anyone got any ideas of where I’m going wrong? The combination of conditions is clearly wrong, and I can’t work out how to make each side of the or statement mutually exclusive
Even if I try to evaluate only the if(sn = ‘blah’) I get the wrong result for obvious reasons, but can’t think of a sane way to express it.
Many thanks
Scott

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T06:05:35+00:00Added an answer on June 7, 2026 at 6:05 am

    If there is is no contract with a serial number of C20050, this query will return all rows, otherwise, it will return only one row where serial_no is C20050:

    SELECT a.*
    FROM cust_site_contract a
    INNER JOIN
    (
        SELECT COUNT(*) AS rowexists
        FROM cust_site_contract
        WHERE serial_no = 'C20050'
    ) b ON b.rowexists = 0
    
    UNION ALL
    
    (
        SELECT *
        FROM cust_site_contract 
        WHERE serial_no = 'C20050'
        LIMIT 1
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
Thanks in advance for your help. I have a need within an application to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.