Removing all rows with specific value in MySQL - mysql

I have a table named contacts
id name value
1 a x
2 b c
3 c x
4 d x
5 e x
How I want to delete the rows that contain value of x ?

A simple SQL query will do.
DELETE * FROM contacts WHERE value='x'

Related

How to perform a many-to-many or (at least) a outer-join in SPSS

usually I use [R] for my data analysis, but these days I have to use SPSS. I was expecting that data manipulation might get a little bit more difficult this way, but after my first day I kind of surrender :D and I really would appreciate some help ...
My problem is the following:
I have two data sets, which have an ID number. Neither data sets have a unique ID (in one data set, which should have unique IDs, there is kind of a duplicated row)
In a perfect world I would like to keep this duplicated row and simply perform a many-to-many-join. But I accepted, that I might have to delete this "bad" row (in dataset A) and perform a 1:many-join (join dataset B to dataset A, which contains the unique IDs).
If I run the join (and accept that it seems not to be possible to run a 1:many, but only a many:1-join), I have the problem, that I lose IDs. If I join dataset A to dataset B I lose all cases, that are not part of dataset B. But I really would like to have both IDs like in a full join or something.
Do you know if there is (kind of) a simple solution to my problem?
Example:
dataset A:
ID
VAL1
1
A
1
B
2
D
3
K
4
A
dataset B:
ID
VAL2
1
g
2
k
4
a
5
c
5
d
5
a
2
x
expected result (best solution):
ID
VAL1
VAL2
1
A
g
1
B
g
2
D
k
3
K
NA
4
A
a
2
D
x
expected result (second best solution):
ID
VAL1
VAL2
1
A
g
2
D
k
3
K
NA
4
A
a
5
NA
c
5
NA
d
5
NA
a
2
D
x
what I get (worst solution):
ID
VAL1
VAL2
1
A
g
2
D
k
4
A
a
5
NA
c
5
NA
d
5
NA
a
2
D
x
From your example It looks like what you need is a full many to many join, based on the ID's existing in dataset A. You can get this by creating a full Cartesian-Product of the two dataset, using dataset A as the first\left dataset.
The following syntax assumes you have the STATS CARTPROD extention command installed. If you don't you can see here about installing it.
First I'll recreate your example to demonstrate on:
dataset close all.
data list list/id1 vl1 (2F3) .
begin data
1 232
1 433
2 456
3 246
4 468
end data.
dataset name aaa.
data list list/id2 vl2 (2F3) .
begin data
1 111
2 222
4 333
5 444
5 555
5 666
2 777
3 888
end data.
dataset name bbb.
Now the actual work is fairly simple:
DATASET ACTIVATE aaa.
STATS CARTPROD VAR1=id1 vl1 INPUT2=bbb VAR2=id2 vl2
/SAVE OUTFILE="C:\somepath\yourcartesianproduct.sav".
* The new dataset now contains all possible combinations of rows in the two datasets.
* we will select only the relevant combinations, where the two ID's match.
select if id1=id2.
exe.

Splitting string into single values in Mysql

I have a table (table_string) & data as below, basically trying to split the string values into single value and store in a separate table.
ID Name ADD
1 a,b,c d,e,f
2 x,y,c n,e,f
3 n,b,c d,e,f
4 x,y,c n,e,f
After transformation the table data looks like the below .
**ID** Name ADD
1 a d
1 b e
1 c f
2 x n
2 y e
2 c f and so on....
SELECT regexp_substr(Name, '[^,]+', 1, LEVEL)
FROM dual
CONNECT BY regexp_substr(Name, '[^,]+', 1, LEVEL) IS NOT NULL

Access Query - Return all rows with matching number until another field has a specific value

I currently have a quite large Excel sheet with a lot of VBA that I would like to move into Access if possible to speed it up using queries instead of a bunch of loops, if statements, etc. The main stopping point right now is working with the orders data.
The data is downloaded in a format similar to below (I've simplified it greatly):
Order Number Operation Sequence
A1 0 1
A1 0 2
A1 L 3
A1 L 4
A2 L 1
A2 0 2
A3 L 1
A4 0 1
A4 L 2
A4 L 3
A4 L 4
In Excel, I am able to just loop through the rows using the order number and stop when the operation is "L". I would like to go through each row using the order number and delete all further sequences after the first "L" is reached. The expected return from the top table would be:
Order Number Operation Sequence
A1 0 1
A1 0 2
A1 L 3
A2 L 1
A3 L 1
A4 0 1
A4 L 2
The blank/missing rows are what should be deleted from the table.
Current table
I have not tested this but it will give you a good starting point. It assumes your Access table is properly imported with a RecordID Primary Key:
DELETE * FROM OrderData WHERE RecordID IN (
SELECT RecordID FROM OrderData INNER JOIN
(SELECT OrderNumber, MIN(Sequence) AS LGS FROM OrderData WHERE Operation='L' GROUP BY OrderNumber) LGR
ON OrderData.OrderNumber=LGR.OrderNumber AND OrderData.Sequence>LGR.LGS )
What it does is first get the MIN(Sequence) with 'L' Operation for each Order Number, then get all the RecordIDs where Sequence is higher than that and use it as a DELETE condition.

How to add dynamic range to database (store the ranges in a table)

Table (CostTitle)
Id_ _costTitle_
1 A
2 B
3 C
4 D
5 E
6 F
A Refers numbers between 0-99
B Refers numbers between 100-199
C Refers numbers between 200-299
D Refers numbers between 300-399
E Refers numbers between 400-499
F Refers numbers between 500-599
costCode will be base on costTitle's refers numbers
Table (CostCode)
Id_ _costTitle_ _costCode_ _costProductTitle_
1 A 12 productX
2 B 111 productY
3 B 142 productZ
4 C 201 productK
5 F 511 productL
6 F 582 productM
I am trying to add product and assign dynamically cost code.
Thanks for advance
I suppose you want to store the ranges in a table. So you need a BEFORE INSERT trigger, which sets new.costTitle. Triggers are explained here:
http://dev.mysql.com/doc/refman/5.6/en/create-trigger.html
MariaDB offers an alternative: dynamic columns. However, because of the limits of this feature, you cannot store the ranges in a different table. You will need to hardcode the ranges in the virtual column definition, which doesn't seem to me a great idea (but you decide, of course).
https://mariadb.com/kb/en/mariadb/virtual-computed-columns/

Display result for mysql query

table A
id name group_id email
1 a 1 a#g.com
2 b 3,4 b#g.com
3 c 1,3,4 c#g.com
4 d 2,5,1 d#g.com
table b
id user user_group_id
1 x 1,3
The table structure is as above
OUTPUT: if i search for user_group_id (from table B) for 1,3 in table A then i should get 4 email addresses i.e a#g.com,b#g.com,c#g.com,d#g.com. Since 1 is present in 3 rows in table A and 3 is present in 2 rows.
While I don't exactly understand what your problem is, I have the impression that your table structure is not properly normalized.
Your table b should have two entries:
id user user_group_id
1 x 1
2 x 3
In this case, you can properly join your tables and get all answers when querying for a certain user name.