MySQL: Manipulate column data in a query with SELECT * - mysql

Is it possible to manipulate column data in a query where you do SELECT *?
table
id - delivery
1 - 0
2 - 0
3 - 12
Something like:
SELECT *, IF(delivery = '0', '9') FROM table
Or do you have to select the columns individually?
This would be a total mess for me since the actual table has like 40 columns.
EDIT: I didnt make myself clear what i wanted. The result should be:
id - delivery
1 - 9
2 - 9
3 - 12

Try this:
SELECT *, case delivery when '0' THEN '9' ELSE delivery END FROM table

Try this
,
Select
column1,
column2,
CASE WHEN delivery=0 THEN 9
ELSE delivery
END DELIVERY
from TABLE

Related

MySQL query with multiple BETWEEN is always NULL

Maybe I'm missing the obvious here, but why is this query always NULL?
SELECT ROUND(AVG(`viewer_count`),0) AS avg_viewer FROM `table`
WHERE
(id BETWEEN 1 AND 8)
AND
(id BETWEEN 26 AND 32)
Isn't it possible to get the average of multiple ranges like this?
Since it is not possible for an id to be between 1 and 8 and at the same time between 26 and 32, the query is returning NULL as no rows were matched. If you use OR as squemeamish suggested in the comment, you will get the average over both ranges.
SELECT ROUND(AVG(CASE WHEN id BETWEEN 1 AND 8 THEN `viewer_count` END),0) AS avg_viewer1_8
, ROUND(AVG(CASE WHEN id BETWEEN 26 AND 32 THEN `viewer_count` END),0) AS avg_viewer26_32
FROM `table`

Mysql search text in one column

For Example our table;
İd--------Price---------Level
1 ------100-300 ------ 1,2
2 ---------200----------1
3 ------100-280--------1,3
We want search a price value is 110. 110 is between 100-300 and 100-280 so id 1 and id 2 must listed. Can we write this query with my-sql?.
Additional , we want search price and level value. Price 110 and level 2 searching. Can we write this query with my-sql?.
Thank You
Remember that database tables should be created with the idea that it will satisfy your query needs. It doesn't make sense to have a table with a price "100-300" which represents a String (or in mysql a VARCHAR) and you want to treat this as a number. So what to do?
1) The first thing i would do is re write my table schema having a minPrice and maxPrice fields, so this way you could have this:
İd----minPrice---maxPrice------level
1 ------100---------300 ------ 1,2
2 ------200---------200----------1
3 ------100---------280--------1,3
2) Then your query would be like:
SELECT id FROM Mytable x WHERE myValue >= x.minPrice AND myValue <= x.maxPrice
3) In case you also want to look for a level value. you would do:
SELECT id FROM Mytable x WHERE myValue >= x.minPrice AND myValue <= x.maxPrice AND myLevelValue IN (x.level)
Use two columns for the price ...
ID FROM TO LEVEL
1 100 300 1,2
2 200 200 1
3 100 280 1,3
Then SQL:
SELECT `level` FROM `table`
WHERE X >= `from` AND X <= `to`

Spliting MySql value with default function in mysql?

I Have a sitatation like the following:
In MySql Table Entries:
img_id name
1 aa.jpg
2 aab.mpeg
3 aabc.jpg
4 aabd.jpg
5 aabn.jpg
6 aabf.jpg
7 aadf.jpg
8 aacf.jpg
I want the count after splitting thew above values........
like
".jpg"=>7
".mpeg"=>1
SELECT RIGHT(Name, LOCATE('.', REVERSE(Name)) - 1) Format,
COUNT(*) TotalCOunt
FROM TableName
GROUP BY RIGHT(Name, LOCATE('.', REVERSE(Name)) - 1)
SQLFiddle Demo
Consider normalizing the table. In the long run, this will perform slow.

Unable to apply WHERE/AND on MySQL table with 2 columns on MAMP

I thought I had a very simple query to perform, but I can't seem to make it work.
I have this table with 2 columns:
version_id trim_id
1 15
1 25
1 28
1 30
1 35
2 12
2 25
2 33
2 48
3 11
3 25
3 30
3 32
I am trying to get any version-id's that have say a sub-set of trim_id's. Let's say all version_id's that have trim_id's 25 and 30. My obvious attempt was :
SELECT * FROM table WHERE trim_id=25 AND trim_id=30
I was expecting to have version_id 1 and 3 as a result, but instead I get nothing.
I am working with the latest version of MAMP, which has some odd behavior, like in this case it just tells me its 'LOADING' and never gives me an error message or something. But that's normally the case when there is no data to return.
This is InnoDB, if that helps.
Thanks for your input.
Your query does not work because you are using AND and the trim_id cannot have two different values at the same time, so you need to apply Relational Division to get the result.
You will need to use something similar to the following:
SELECT version_id
FROM yourtable
WHERE trim_id in (25, 30)
group by version_id
having count(distinct trim_id) = 2
See SQL Fiddle with Demo.
This will return the version_id values that have both 25 and 30. Then if you wanted to include additional columns in the final result, you can expand the query to:
select t1.version_id, t1.trim_id
from yourtable t1
where exists (SELECT t2.version_id
FROM yourtable t2
WHERE t2.trim_id in (25, 30)
and t1.version_id = t2.version_id
group by t2.version_id
having count(distinct t2.trim_id) = 2);
See SQL Fiddle with Demo
SELECT *
FROM table
WHERE trim_id IN(25,30)

MySQL How To Use Main Select Value As Subquery Argument?

I am trying to construct a query that is a bit more complicated than anything I've done in my limited experience with databases.
TABLE:
id - data - type - data3
1 - hello - 1 - 1
2 - goodbye - 1 - 1
3 - goodbye - 1 - 2
4 - goodbye - 2 - 1
5 - hello - 2 - 1
The goal is to do 4 things:
GROUP the results by "data", but only return one result/row of each
data type.
COUNT the total number of each "data GROUP and return this number.
Do this for both "type"=1 and "type"=2, though I only need each
"data" GROUP item once.
the ability to sort results based on each SELECT item.
So the final result returned should be (sorry to be confusing!):
data, COUNT(data["type"]=1), COUNT(data["type"]=2 AND data["data"] = data)
So, for the sample table above, desired results would be:
loop 1 - hello, 2, 1
loop 2 - goodbye, 3, 1
Then, ideally, I could sort results by any of these.
This is the query I was trying to construct before resorting to posting this, I don't think it's even close to being correct, but it may help illustrate what I'm trying to achieve a bit better:
SELECT
(
SELECT `clicks_network_subid_data`, COUNT(*)
FROM track_clicks
WHERE `clicks_campaign_id`='$id' AND `clicks_click_type` = '1'
) AS keywords,
(
SELECT COUNT(*)
FROM track_clicks
WHERE `clicks_campaign_id`='$id' AND `clicks_click_type` = '2' AND `clicks_network_subid_data` = keywords.clicks_network_subid_data
) AS offer_clicks
GROUP BY keywords.clicks_network_subid_data
ORDER BY keywords.COUNT(*) DESC
I also need to do a JOIN on another table to grab one more piece of data, but I think I can handle that once I get this part figured out.
You can use an IF-function for this
SELECT `clicks_network_subid_data`,
SUM(IF(clicks_click_type` == '1',1,0)) as keywords,
SUM(IF(clicks_click_type` == '2',1,0)) as offer_clicks,
FROM track_clicks
GROUP BY clicks_network_subid_data
ORDER BY clicks_network_subid_data DESC
You can do this using GROUP BY:
SELECT data, COUNT(*) AS cnt FROM `table` GROUP BY type ORDER BY COUNT(*)
Ordering might become a little slow, as this is a calculated field, but if you don't have a large result set then you are good to go.
First of all your question is bit not clear , However, check this query . what I suspect is that you need count results in columns (single ) instead of rows .
select * , count(type_one) as t1_count , count(type_two) as t2_count from (
select data,if(tmp.type=1,1,0) as type_one, if(tmp.type=2,1,0) as type_two from (
select 1 as id , 'hello' as data , 1 as type , 1 as data3 union
select 2 as id , 'goodbye' as data , 1 as type , 1 as data3 union
select 3 as id , 'goodbye' as data , 1 as type , 2 as data3 union
select 4 as id , 'goodbye' as data , 2 as type , 1 as data3 union
select 5 as id , 'hello' as data , 2 as type , 1 as data3
) tmp
) tmp2
group by tmp2.type_one ;
let me know if this works for you
cheers :)