REPLACE function but with entire column - MySQL - mysql

I learned about the select replace('string','to be replaced','with this') function, but I'm wondering is there a way I can do something to a whole column, like this
select replace(table.column1,'replace this', 'with this');
Right now, the command is returning Query 1 ERROR: Unknown table 'private_db' in field list

You need a FROM clause.
SELECT replace(column1, 'replace this', 'with this') AS new_column1
FROM yourTable
This will return all the values in column1 with the replacement made.

Related

I have a issue with the if statement in mysql

I want to execute a if statement which is like this
select IF ( quantity_wanted!=3,(select name from grocerywanted),Null) AS message from grocerywanted;
I want here to check the quantity_wanted column if it contains a value except 3 run the select statement
Your query is using same table name not as sub query. I think you should use 'name' in replace of '(select name from grocerywanted)'. You will get your desired output.
select IF ( quantity_wanted!=3,name,Null) AS message from grocerywanted;

How can I insert data into a table from a query which return multiple statement?

INSERT INTO admin(UserID,Username,InitialBid) select ID,username,(select
case
when experience = 'Fundamental' then '1'
when experience = 'Novice' then '2'
when experience = 'Intermediate' then '3'
when experience = 'Advanced' then '4'
when experience = 'Expert' then '5'
end as intbid
from user_details ) from user_details;
In this code I want to add data from one table to another along with a conditional statement column as a sub-query. As it returns multiple rows at a time so it is not able to execute this query.
What should I write that we got value in InitialBid column corresponding to its value?
I really doubt that you want a subquery at all. Doesn't this do what you want?
insert into admin (UserID, Username, InitialBid)
select id, username,
case experience
when 'Fundamental' then 1
when 'Novice' then 2
when 'Intermediate' then 3
when 'Advanced' then 4
when 'Expert' then 5
end as intbid
from user_details;
This inserts one row in the target table for each row in user_details, while transcoding the experience to a literal number.
Notes:
a short-circuit case expression is good enough in this situation (the column is listed only once, at the beginning of the expression)
initialBid looks like a numeric column; if so, remove the single quotes around the literal values that are generated by the case expression, so the database does not need to implicitly cast them.

Copy data from one table to another ERROR 1136

I'm trying to copy data from one table to another.
For that matter I'm using this:
INSERT INTO radacct_2011_2012 SELECT 'RadAcctId, AcctSessionId, AcctUniqueId, UserName, Realm, NASIPAddress, NASPortId, NASPortType, AcctStartTime, AcctStopTime, AcctSessionTime, AcctAuthentic, ConnectInfo_start, ConnectInfo_stop, AcctInputOctets, AcctOutputOctets, CalledStationId, CallingStationId, AcctTerminateCause, ServiceType, FramedProtocol, FramedIPAddress, AcctStartDelay, AcctStopDelay' from radacct where 'AcctStartTime' >= '2011' AND 'AcctStartTime' <= '2012';
When I try to run it I get the following error:
ERROR 1136 (21S01): Column count doesn't match value count at row 1
I'm reading about it and none of the solutions that I found helped me.
You only SELECT one value because you escape the whole column list with '. So MySQL interpret this as a string value, not a column list.
So you should use the following query instead:
INSERT INTO `radacct_2011_2012`
SELECT `RadAcctId`, `AcctSessionId`, `AcctUniqueId`, `UserName`, `Realm`, `NASIPAddress`, `NASPortId`, `NASPortType`, `AcctStartTime`, `AcctStopTime`, `AcctSessionTime`, `AcctAuthentic`, `ConnectInfo_start`, `ConnectInfo_stop`, `AcctInputOctets`, `AcctOutputOctets`, `CalledStationId`, `CallingStationId`, `AcctTerminateCause`, `ServiceType`, `FramedProtocol`, `FramedIPAddress`, `AcctStartDelay`, `AcctStopDelay`
FROM `radacct`
WHERE `AcctStartTime` >= '2011' AND `AcctStartTime` <= '2012';
There is a general problem on your query using the ' in the wrong situations. You are using ' on column names of the WHERE part too. MySQL compares 'AcctStartTime' >= '2011' as false because the string values (in this example AcctStartTime and 2011) are never equal. If you want to escape column names you have to use the backtick (``).
You should read When to use single quotes, double quotes, and backticks in MySQL to get the difference between the different escape possibilities.
Tim provided a demo to confirm that double quotes (and single quotes too) can not be used to escape column names.
Please check the no of columns in the first table and check the no of columns in the second table.
You should also match the order of the columns. i.e.
Table 1
col1 col2 col3 col4
Table 2
col1 col2 col3 col4
insert into table1 select col1,col2,col3,col4 from table2 where col1 = '2';
It will give error if the order is not match or the no of column is greater or less in the first table.

Running a SQL SELECT statement against a MYSQL column of SET type

I'm trying to run a SQL SELECT statement against a column that is of type SET. The table is called myTable and the columns in myTable are called base_props and names. The base_props column is of type SET. The values in base_prop are vb,nt, cnt,poss and loc. So I would like to SELECT entries from the column 'name' where base_props have both the values, vb and poss. The results I'm looking to get may have values other than just vb and poss. So to be clear I would like to select all entries that have the values vb and poss regardless if they have other values as well. I've tried the following SQL queries but I can't get the desired results.
SELECT name from myTable WHERE base_props = 'vb' AND base_props = 'poss'
That query returns an empty result set. I've tried using FIND_IN_SET() and IN() but I couldn't get anywhere with that. I've written SQL statements before but never had to deal with columns that are type SET. Any help is appreciated.
The only thing I can come up with is using the LIKE keyword:
SELECT name FROM myTable WHERE (base_props LIKE '%vb%' AND base_props LIKE '%poss%');
This will make sure both vb and cnt are in the base_props column. Of course you can use cnt, nt and loc in there, or any number of base_props values in the sql, just add more AND statements.
OR as a deleted answer by samitha pointed out, you can use FIND_IN_SET:
SELECT name from myTable WHERE FIND_IN_SET('vb', base_props) AND FIND_IN_SET('poss', base_props);
Comment (by spencer7593): "both of these work, but there is a slight difference. The LIKE operator will actually match any member that includes the search string anywhere in a term; the FIND_IN_SET function will only match an exact member. It's also possible to search for members in set by the order they appear in the SET definition, using the MySQL BITAND operator: for example, to match the 1st and 4th members of the set: WHERE base_props & 1 AND base_props & 8". So for example, if you have 'a' and 'aaa' in your set, then using the LIKE "%a%" method will also return rows containing 'aaa'.
Conclusion: use the FIND_IN_SET solution since it will work for all cases.
FIND_IN_SET return index, Try this
SELECT name from myTable WHERE FIND_IN_SET(base_props, 'vb') > 0 AND
FIND_IN_SET(base_props, 'poss') > 0

MySQL Unexpected Result from "in (' ' or ' ')"

What I'm Using: The most recent MySQL on Ubuntu 12.
The Set Up: Suppose I have a table "EmployeePayment" with "Name" and "Hours" for each employee. Suppose I already have it populated with values.
The Question: When I use the command
select * from EmployeePayment where Name in ('');
I get the empty set, as I'd expect. But, when I use
select * from EmployeePayment where Name in ('' or '');
I get the entire table returned. Moreover, if I'm picky and put in the command
select Name, SUM(Hours) from EmployeePayment where Name in ('' or '');
then it only returns whatever is the top name from the table. What's happening with this "in" command?
First off, you need to get rid of the or, the proper syntax for the in clause uses commas to separate the possibilities, such as:
sql> select name from people where status in ('intelligent', 'good looking')
pax
1 row returned
What your current variant is doing is applying the or operator to give you a one-element in-list. See here for more detail.
The reason why you're only getting one row for the aggregated query is because you have no group by clause, so you're grouping all rows. Most DBMS' would then complain about having a non-aggregated column that isn't part of the grouping, but MySQL is a bit fancy-free and footloose with the rules in that regard.
It's obviously grouping over the whole table (as it should) but applying some default aggregating function to the name (which it probably shouldn't, but does according to its documentation).
This MySQL extension is covered here but heed the warning: MySQL can choose any of the myriad possible values for these non-aggregated, non-group-by columns, so it's more useful when you know that all the rows in a given group share the same value for the column.
You're effectively doing this:
select * from EmployeePayment where Name in (0);
The OR expression evaluates to 0, and WHERE Name IN (0); returns all rows. You have to use the proper IN syntax as suggested in the other answers:
SELECT * FROM EmployeePayment WHERE Name IN ('foo', 'bar');
IN uses comma separated values, for example: WHERE Name IN ('tim','beth')
So try WHERE Name IN ('','');
But more importantly, why would you want to check where a value is empty or empty? Or was that just to get the question across?
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in