Please how can I update different columns base on different and specific conditions. For example:
UPDATE table
SET col1 = val1 WHERE col1 > 2
SET col2 = val2 WHERE col2 > 1
Is is possible to write an SQL UPDATE statement like this
Where different columns will be updated base on separate conditions?
Use case:
UPDATE table
SET col1 = (CASE WHEN col1 > 2 THEN val1 ELSE col1 END),
col2 = (CASE WHEN col2 > 1 THEN val2 ELSE col2 END);
You can also add WHERE col1 > 2 or col2 > 1 so MySQL does not attempt to update all rows.
I have two columns that store values(point values).
How do I select where my given number is between the values in the two columns ?
I hope you can get it by doing this:
Lets your number is $your_number and table name is your_table
So your query would be:
SELECT * FROM your_table WHERE $your_number BETWEEN col1 AND col2;
Or if it is ensured that col1 < col2
SELECT * FROM your_table WHERE $your_number >= col1 AND $your_number <= col2;
I want to execute a query where I can find one ID in a list of ID.
table user
id_user | name | id_site
-------------------------
1 | james | 1, 2, 3
1 | brad | 1, 3
1 | suko | 4, 5
and my query (doesn't work)
SELECT * FROM `user` WHERE 3 IN (`id_site`)
This query work (but doesn't do the job)
SELECT * FROM `user` WHERE 3 IN (1, 2, 3, 4, 6)
That's not how IN works. I can't be bothered to explain why, just read the docs
Try this:
SELECT * FROM `user` WHERE FIND_IN_SET(3,`id_site`)
Note that this requires your data to be 1,2,3, 1,3 and 4,5 (ie no spaces). If this is not an option, try:
SELECT * FROM `user` WHERE FIND_IN_SET(3,REPLACE(`id_site`,' ',''))
Alternatively, consider restructuring your database. Namely:
CREATE TABLE `user_site_links` (
`id_user` INT UNSIGNED NOT NULL,
`id_site` INT UNSIGNED NOT NULL,
PRIMARY KEY (`user_id`,`site_id`)
);
INSERT INTO `user_site_links` VALUES
(1,1), (1,2), (1,3),
(2,1), (2,3),
(3,4), (3,5);
SELECT * FROM `user` JOIN `user_site_links` USING (`id_user`) WHERE `id_site` = 3;
Try this: FIND_IN_SET(str,strlist)
NO! For relation databases
Your table doesn't comfort first normal form ("each attribute contains only atomic values, and the value of each attribute contains only a single value from that domain") of a database and you:
use string field to contain numbers
store multiple values in one field
To work with field like this you would have to use FIND_IN_SET() or store data like ,1,2,3, (note colons or semicolons or other separator in the beginning and in the end) and use LIKE "%,7,%" to work in every case. This way it's not possible to use indexes[1][2].
Use relation table to do this:
CREATE TABLE user_on_sites(
user_id INT,
site_id INT,
PRIMARY KEY (user_id, site_id),
INDEX (user_id),
INDEX (site_id)
);
And join tables:
SELECT u.id, u.name, uos.site_id
FROM user_on_sites AS uos
INNER JOIN user AS u ON uos.user_id = user.id
WHERE uos.site_id = 3;
This way you can search efficiently using indexes.
The problem is that you are searching within several lists.
You need something more like:
SELECT * FROM `user` WHERE id_site LIKE '%3%';
However, that will also select 33, 333 and 345 so you want some more advanced text parsing.
The WHERE IN clause is useful to replace many OR conditions.
For exemple
SELECT * FROM `user` WHERE id IN (1,2,3,4)
is cleaner than
SELECT * FROM `user` WHERE id=1 OR id=2 OR id=3 OR id=4
You're just trying to use it in a wrong way.
Correct way :
WHERE `field` IN (list_item1, list_item2 [, list_itemX])
I would like some help that has had me stumped for two days. I need to retrieve data from a database and order it by column1 when it isn't empty and then the rest of the result by column2
column1 column2
1 11
2 12
3 13
14
15
16
Required result
1,2,3,14,15,16
I've tried numerous approaches, my latest failed attempt being
$SQL = "SELECT * FROM table ORDER BY COALESCE(column1, column2) DESC";
and
$SQL = "SELECT * FROM table ORDER BY COALESCE(column1, column2) ASC";
My above SQL is returning NULL value column1 above column2
This should work:
$SQL = "SELECT * FROM table ORDER BY COALESCE(NULLIF(Other,''), column2) DESC";
I saw it here: SQL Coalesce with empty string
coalesce() would only work if the "empty" values in column1 are actually NULL. Empty strings will not trigger a coalesce() operation.
Beyond that, your query will NOT work. You cannot do a select * with two columns and somehow magically get one single column in the result. For this you'll need a UNION query:
(SELECT column1 AS col FROM yourtable)
UNION ALL
(SELECT column2 AS col FROM yourtable)
ORDER BY col
If you want 1 column, you could try a combination of NULLIF and COALESCE, that should account for both empty and null values
SELECT COALESCE(NULLIF(column1, ''), column2) AS COL
FROM table
SQLFiddle Demo
In case you actually want all of the numbers on a single result row, separated by commas, you can use GROUP_CONCAT along with the previous code:
SELECT GROUP_CONCAT(COALESCE(NULLIF(column1, ''), column2)) AS col
FROM table
SQLFiddle Demo2
Old question but this solution worked for me:
$SQL = "SELECT * FROM table ORDER BY COALESCE(NULLIF(column1, ''), column2)";
You should be able to use CASE like so:
SELECT *
FROM table
ORDER BY
CASE WHEN LENGTH(column1) IS >0 THEN column1
ELSE column2 END
ASC
http://dev.mysql.com/doc/refman/5.0/en/case.html
I dont know if this is possible:
SELECT * FROM stuff WHERE barcode = '123123' OR product like '%123123%'
Lets say this returns 10 rows. Two out of the 10 rows got returned because they had 123123 in barcode.
Now is it possible somehow to make a alias/variable to know that it comes because the it matched the barcode.
So i can do:
if($MatchWasFromBarcode) {.... }
Or will i have to make 2 queries for this?
Assuming you are using MySQL:
SELECT
barcode = '123123' AS MatchWasFromBarcode,
col1, col2, ..., coln
FROM stuff
WHERE barcode = '123123' OR product like '%123123%'
For other databases change the second line to this:
CASE WHEN barcode = '123123' THEN 1 ELSE 0 END AS MatchWasFromBarcode,
SELECT case when barcode='your_value' then 1 else 0 end as matches_bar_code
, col1
, col2
, col3
, col4
FROM stuff
WHERE barcode = '123123'
OR product like '%123123%'
Don't do select *. Bad practice. List the columns you need.