I need to get my data when userID=4 and status= In use or Pending or Deleted.
But I am getting extra data not sure why.
I am getting extra data from
userid=3
MySQL query is:
SELECT * FROM `registered_bicycle`
WHERE (`userID`='4')
AND (`status`='In Use')
OR (`status`='Pending')
OR (`status`='Deleted')
You have to use (...) arround the OR Statements:
SELECT * FROM `registered_bicycle`
WHERE (`userID`='4')
AND (`status`='In Use'
OR `status`='Pending'
OR `status`='Deleted')
or use IN function
Put the part after the AND between braces, otherwise it will return data with userID=4 OR status=xy
SELECT * FROM `registered_bicycle` WHERE (`userID`='4') AND (
(`status`='In Use')
OR (`status`='Pending')
OR (`status`='Deleted')
)
Try this instead:
SELECT
*
FROM
`registered_bicycle`
WHERE `userID` = '4'
AND `status` IN('In Use','Pending','Deleted')
Your and/or logic got mixed up because of lacking proper parentheses. Also you can use IN instead of multiple OR clauses.
Btw, IN is just the short form of OR
try this:
SELECT * FROM `registered_bicycle`
WHERE (`userID`='4')
AND `status`IN('In Use','Pending','Deleted')
Related
My problem is simple, I'm using the query
(ReturnItm.PackQty * ReturnItm.ReturnQty) as Total_Qty,
CASE
WHEN ReturnItm.UOM='U' THEN
IF(Inventory.Price=0,Inventory.Pricec,Inventory.Price)
WHEN ReturnItm.UOM='P' THEN Inventory.Pricep
ELSE Inventory.Pricec
END AS Price
So, how to multiple that Total_Qty and Price?
Is it just like this?
(Total_Qty*Price) as Total_price
Help me, please
To select this product in the same select clause, you'll have to repeat the full expressions:
SELECT
Test.1,
Test.2,
Test.3,
Test.4,
(Test.1 * Test.2) AS TestNew1,
(Test.3 * Test.4) AS TestNew2,
Test.1 * Test.2 * Test.3 * Test.4 AS TotalTest
FROM Test;
Use a sub-query...
SELECT
*,
(TestNew1 * TestNew2) as TotalTest
FROM
(
SELECT
Test.1,
Test.2,
Test.3,
Test.4,
(Test.1 * Test.2) as TestNew1,
(Test.3 * Test.4) as TestNew2
FROM
Test
)
AS x
The inner query acts as a scope within which the new columns can be expressed, and the outer query acts as a scope where there can be referrenced.
Also, in terms of performance, note that SQL isn't executed as written. It's a "declarative" language; it's a means of expressing the functionality, and then the DBMS turns than in to an actual execution plan.
select * from user_levels
join collectors_users on user_levels.id = collectors_users.user_level
where collectors_users.username = 'testuser'
I want it to pull everything from user_levels and nothing from collectors_users. But it's pulling from both. How do I correct the statement?
Instead of select * specify what you actually want and use select user_levels.* or even better skip the * and write out the columns you want (and consider using aliases to keep it short and tidy): select ul.col1, ul.col2 ... from userlevels ul join ...
It is getting all the data as the '*' means 'all' columns. You can limit the columns for just one table by specifying the table:
select user_levels.*
from user_levels
join collectors_users on user_levels.id = collectors_users.user_level
where collectors_users.username = 'testuser'
Pro tip: Don't use SELECT * in running software. Instead, be as specific as you can be about the columns you want in your result set.
SELECT user_levels.*
should help a bit.
I might suggest that you use in or exists, because this is more consistent with the intention of the query:
select ul.*
from user_levels ul
where ul.id in (select cu.user_level
from collectors_users cu
where cu.username = 'testuser'
);
In addition, this version will not produce duplicate rows if collectors_users has multiple matching rows for a singel row in user_levels.
Also note the use of table aliases: these make the query easier to write and to read.
I am trying to create a new data extract from a (badly designed) sql database. The customer requires that I add a distinctidentifier which I am attempting to do using the NEWID() function. Unfortunately this leads to multiple duplicate records being returned.
After a bit of research I have found that the NEWID() function does indeed 'undo' the use of the distinct keyword, but I cannot work out why or how to overcome this.
An example of the query I am trying to write is as follows:
select distinct
NEWID() as UUID
,Histo_Results_File.ISRN
,Histo_Results_File.Internal_Patient_No
,Histo_Results_File.Date_of_Birth
,Histo_Result_freetext.histo_report
,Histo_Report.Date_Report_Updated as [Investigation_Result_Date]
from apex.Histo_Results_File
inner join apex.Histo_Report on (Histo_Report.Histo_Results_File = Histo_Results_File.ID)
If I miss out the NEWID() line in the select block, I get 569 records returned, which is correct, but if I include that line then I get in excess of 30,000 which are all duplicates of the original 569 but with different IDs. Can anyone suggest a way around this problem?
Thanks in advance
Use a sub query would be the easiest way to do it.
SELECT NEWID() as UUID
, * -- this is everything from below
FROM (
select distinct
Histo_Results_File.ISRN
,Histo_Results_File.Internal_Patient_No
,Histo_Results_File.Date_of_Birth
,Histo_Result_freetext.histo_report
,Histo_Report.Date_Report_Updated as [Investigation_Result_Date]
from apex.Histo_Results_File
inner join apex.Histo_Report on (Histo_Report.Histo_Results_File = Histo_Results_File.ID)) as mySub
select NEWID() as UUID
,ISRN
,Internal_Patient_No
,Date_of_Birth
,histo_report
,Investigation_Result_Date
from (
select distinct
,Histo_Results_File.ISRN
,Histo_Results_File.Internal_Patient_No
,Histo_Results_File.Date_of_Birth
,Histo_Result_freetext.histo_report
,Histo_Report.Date_Report_Updated as [Investigation_Result_Date]
from apex.Histo_Results_File
inner join apex.Histo_Report on (Histo_Report.Histo_Results_File = Histo_Results_File.ID)) t
You can use a sub-query to get around the issue, something like.....
SELECT NEWID() as UUID
,*
FROM (
select distinct
Histo_Results_File.ISRN
,Histo_Results_File.Internal_Patient_No
,Histo_Results_File.Date_of_Birth
,Histo_Result_freetext.histo_report
,Histo_Report.Date_Report_Updated as [Investigation_Result_Date]
from apex.Histo_Results_File
inner join apex.Histo_Report
on (Histo_Report.Histo_Results_File = Histo_Results_File.ID)
) t
I have this query:
select feature_requests.*,
from feature_requests
where feature_requests.status in ('open','closed','indevelopment')
I also have another status - denied.
I need to also select all rows with status denied but another column on my features request table must equal something.
So something that does this:
select feature_requests.*,
from feature_requests
where feature_requests.status in ('open','closed','indevelopment','denied') and
if status = denied, instance_id = ?
Not sure of the correct syntax. Thanks for any help :)
The WHERE clause is the correct place to put these kind of conditions, but with a few differences:
SELECT `fr`.*
FROM `feature_requests` fr
WHERE (`fr`.`status` IN ('open','closed','indevelopment')) OR
((`fr`.`status` = 'denied') AND (`fr`.`instance_id` = ?))
P.S - Notice I'm using an alias for feature_requests called fr instead of writing the whole name again and again. And You don't have to write its name at all because you're using only one table in your query, but I would still use it because it reduces the chances of future mistakes.
For further reading - SELECT Syntax
From rusty memory, you're probably wanting something like this
SELECT feature_requests.* FROM feature_requests
WHERE feature_requests.status IN ('open', 'closed', 'indevelopment') OR
(feature_requests.status='denied' AND instance_id = ???)
What you have right now is pretty close.
http://www.tutorialspoint.com/sql/sql-and-or-clauses.htm
This should work:
SELECT
feature_requests.*
FROM
feature_requests
WHERE
feature_requests.status IN ('open','closed','indevelopment') OR (
feature_requests.status='denied' AND
instance_id=?
)
This can also be written without listing the table name over and over if it is the only table that you are using like this:
SELECT
*
FROM
feature_requests
WHERE
status IN ('open','closed','indevelopment') OR (
status='denied' AND
instance_id=?
)
When using AND and/or OR in your where clause please also remember to use parenthesis ( ) to show your actual meaning even when you know what takes precedence between the AND and OR. For more information on precedence of operators with MySQl Example:
color=blue AND shape=circle OR type=ball
means
(color=blue AND shape=cirlce) OR type=ball
but could easily be misinterpreted as
color=blue AND (shape=circle OR type=ball)
I have these text in my db,
categories_posts
categories_news
posts_add
news_add
And I don't want to select the rows with categories, I use a query something like this,
SELECT *
FROM developer_configurations_cms
WHERE developer_configurations_cms.cat_id = '1'
AND developer_configurations_cms.cfg_variables LIKE '%parent_id=2%'
AND developer_configurations_cms.cfg_name_unique NOT LIKE '%categories%'
but it returns these two in the output as well...
categories_posts
categories_news
How can I ignore them in my query?
Thanks.
categories_posts and categories_news start with substring 'categories_' then it is enough to check that developer_configurations_cms.cfg_name_unique starts with 'categories' instead of check if it contains the given substring. Translating all that into a query:
SELECT *
FROM developer_configurations_cms
WHERE developer_configurations_cms.cat_id = '1'
AND developer_configurations_cms.cfg_variables LIKE '%parent_id=2%'
AND developer_configurations_cms.cfg_name_unique NOT LIKE 'categories%'
I don't know why
cfg_name_unique NOT LIKE '%categories%'
still returns those two values, but maybe exclude them explicit:
SELECT *
FROM developer_configurations_cms
WHERE developer_configurations_cms.cat_id = '1'
AND developer_configurations_cms.cfg_variables LIKE '%parent_id=2%'
AND developer_configurations_cms.cfg_name_unique NOT LIKE '%categories%'
AND developer_configurations_cms.cfg_name_unique NOT IN ('categories_posts', 'categories_news')