I have column which has values like so foo,bar in mySql Database.
I would like to take out only the foo and discard the rest of the data after comma. How do I get that?
You are looking for substring_index():
select substring_index(col, ',', 1)
You can use Split() function.
Example:
var original_Data = foo,bar ;
var required_Data = original_Data.Split(',')[0];
this will result required_Data = foo;
If you want a "bar" then
var required_Data = original_Data.Split(',')[1];
this will result required_Data = bar;
Related
I got a numeric value in Foo.A and it has its equivalent in Bar but with a string prefix ("Z"). I'm trying to append the "Z" to the Bar.A col value. I also tried with CONCAT but without any success. This following codes returns "Unknown column Z".
UPDATE Foo, Bar
SET Foo.B = Bar.B
WHERE Foo.A = Z + Bar.A
For example 14 (Foo.A) = Z14 (Bar.A).
If your syntax works, then it is likely you are using MySQL. In any case, the problem is that you need quotes around string constants. So try this:
UPDATE Foo join
Bar
on Foo.A = concat('Z', Bar.A)
SET Foo.B = Bar.B;
You should always use single quotes for string and date constants, regardless of the database. That is the ANSI standard and it reduced the possibility of error.
You are missing the single quotes around Z i.e. your code should be:
UPDATE Foo, Bar
SET Foo.B = Bar.B
WHERE Foo.A = CONCAT('Z', Bar.A);
Actually I found it. I missed the "" in CONCAT.
This is working:
UPDATE Foo, Bar
SET Foo.B = Bar.B
WHERE Foo.A = CONCAT("Z", Bar.A)
Where is you join statement you are using table B in the update statement and table A in the where condition. This is not gonna work
source:
constant x,y,
mysql table named 'table1'. It contains column pp.
Need change all values in table1:
pp_new = pp/x*y;
pp_new = round(pp_new) to 0.1.
For example 12.31 = 12.30, 14.56 = 14.50, 55.1245035 = 55.10
12.31 = 12.30, 14.56 = 14.50, 55.1245035 = 55.10
You need to use truncate(number,decimalplaces)
TRUNCATE(1.25864,1)
this will return 1.2
What about something like UPDATE table1 SET pp = round(pp/x*y) ?
I have a function which takes an argument that is used in where clause
function(string x)-->Now this will create a sql query which gives
select colname from tablename where columnname=x;
Now I want this function to give all rows i.e. query equivalent to
select colname from tablename;
when I pass x="All".
I want to create a generic query that when I pass "All" then it should return me all the rows else filter my result.
Just leave the where condition out.
If you really want it that complicated use
where columnname LIKE '%'
which will only filter nulls.
select colname from tablename
where columnname=(case when #x ="All" then columnname
else #x end)
Try this
select colname from tablename where 1=1
hope the above will work
where 1=1 worked for me, Although where clause was being used all records were selected.
You can also try
[any_column_name]=[column_name_in_LHL]
(LHL=left hand side.)
refer my answer for more details
I had the same issue some time ago and this solution worked for me
select colname from tablename where columnname=x or x = 'ALL'
SELECT * FROM table_name WHERE 1;
SELECT * FROM table_name WHERE 2;
SELECT * FROM table_name WHERE 1 = 1;
SELECT * FROM table_name WHERE true;
Any of the above query will return all records from table.
In Node.js where I had to pass conditions as parameter I used it like this.
const queryoptions = req.query.id!=null?{id : req.query.id } : true;
let query = 'SELECT * FROM table_name WHERE ?';
db.query(query,queryoptions,(err,result)=>{
res.send(result);
}
It's unclear what language you're using for your function, but you have to somehow parse the 'All' prior to getting to sql:
public void query(String param) {
String value = "":
switch (param) {
case 'All':
value = "*";
break;
default:
value = param;
}
String sql = "select colname from tablename where colname="+value;
//make the query
}
If you have to allow 'ALL' to be passed through as the parameter value to your function, then you will need to put some manipulation code in your function to construct your SELECT statement accordingly. I.e. You can detect if the parameter has 'ALL' in it and then omit the WHERE clause from your SQL statement. If a value other than 'ALL' comes through, then you can include the WHERE clause along with the relevant filter value from the parameter.
An example of a piece of code to do this would be;
IF x = 'ALL'
THEN
SELECT COLNAME FROM TABLENAME;
ELSE
SELECT COLNAME FROM TABLENAME WHERE COLUMNNAME = X;
END IF;
Give a conditional check in your code(assuming Java) to append the WHERE clause only when x != 'All'
mySqlQuery = "SELECT colname FROM tablename" +
(x.equals("All") ? "" : "WHERE columnname = "+x);
In what order would this be evaluated. My intension is that if it finds either foo or bar, it would also search for lol and rofl.
Is this totally in the woods? And if so, how would one evaluate an expression like that.
The AND operator has higher precedence than OR in MySql, so your current expression evaluates as:
WHERE 'foo' OR ('bar' AND 'lol') OR 'rofl'
Add parentheses to the expression if you want to force the evaluation order:
WHERE ('foo' OR 'bar') AND ('lol' OR 'rofl')
AND will be processed first, after that OR will be processed. So, it will be:
'foo' OR ('bar' AND 'lol') OR 'rofl'
After that, it is left to right order.
take a look at the documentation - AND has a higher precedence, so it would be like this:
WHERE 'foo' OR ( 'bar' AND 'lol' ) OR 'rofl'
SELECT
Id, Name
FROM
TestTable
WHERE
(Name = 'foo') OR (Name = 'bar') AND (Name = 'lol') OR (Name = 'rofl')
Will give you the following result in MS SQL Server:
1 foo
4 rofl
So it seems it will combine bar AND lol and evals foo and rofl seperately like this:
SELECT
Id, Name
FROM
TestTable
WHERE
(Name = 'foo') OR ((Name = 'bar') AND (Name = 'lol')) OR (Name = 'rofl')
What you probably want to do is (technically):
SELECT
Id, Name
FROM
TestTable
WHERE
((Name = 'foo') OR (Name = 'bar')) AND ((Name = 'lol') OR (Name = 'rofl'))
How do I do this
Select top 10 Foo from MyTable
in Linq to SQL?
Use the Take method:
var foo = (from t in MyTable
select t.Foo).Take(10);
In VB LINQ has a take expression:
Dim foo = From t in MyTable _
Take 10 _
Select t.Foo
From the documentation:
Take<TSource> enumerates source and yields elements until count elements have been yielded or source contains no more elements. If count exceeds the number of elements in source, all elements of source are returned.
In VB:
from m in MyTable
take 10
select m.Foo
This assumes that MyTable implements IQueryable. You may have to access that through a DataContext or some other provider.
It also assumes that Foo is a column in MyTable that gets mapped to a property name.
See http://blogs.msdn.com/vbteam/archive/2008/01/08/converting-sql-to-linq-part-7-union-top-subqueries-bill-horst.aspx for more detail.
Use the Take(int n) method:
var q = query.Take(10);
The OP actually mentioned offset as well, so for ex. if you'd like to get the items from 30 to 60, you would do:
var foo = (From t In MyTable
Select t.Foo).Skip(30).Take(30);
Use the "Skip" method for offset.
Use the "Take" method for limit.
#Janei: my first comment here is about your sample ;)
I think if you do like this, you want to take 4, then applying the sort on these 4.
var dados = from d in dc.tbl_News.Take(4)
orderby d.idNews descending
select new
{
d.idNews,
d.titleNews,
d.textNews,
d.dateNews,
d.imgNewsThumb
};
Different than sorting whole tbl_News by idNews descending and then taking 4
var dados = (from d in dc.tbl_News
orderby d.idNews descending
select new
{
d.idNews,
d.titleNews,
d.textNews,
d.dateNews,
d.imgNewsThumb
}).Take(4);
no ? results may be different.
This works well in C#
var q = from m in MyTable.Take(10)
select m.Foo
Whether the take happens on the client or in the db depends on where you apply the take operator. If you apply it before you enumerate the query (i.e. before you use it in a foreach or convert it to a collection) the take will result in the "top n" SQL operator being sent to the db. You can see this if you run SQL profiler. If you apply the take after enumerating the query it will happen on the client, as LINQ will have had to retrieve the data from the database for you to enumerate through it
I do like this:
var dados = from d in dc.tbl_News.Take(4)
orderby d.idNews descending
select new
{
d.idNews,
d.titleNews,
d.textNews,
d.dateNews,
d.imgNewsThumb
};
You would use the Take(N) method.
Taking data of DataBase without sorting is the same as random take
Array oList = ((from m in dc.Reviews
join n in dc.Users on m.authorID equals n.userID
orderby m.createdDate descending
where m.foodID == _id
select new
{
authorID = m.authorID,
createdDate = m.createdDate,
review = m.review1,
author = n.username,
profileImgUrl = n.profileImgUrl
}).Take(2)).ToArray();
I had to use Take(n) method, then transform to list, Worked like a charm:
var listTest = (from x in table1
join y in table2
on x.field1 equals y.field1
orderby x.id descending
select new tempList()
{
field1 = y.field1,
active = x.active
}).Take(10).ToList();
This way it worked for me:
var noticias = from n in db.Noticias.Take(6)
where n.Atv == 1
orderby n.DatHorLan descending
select n;