This question already has answers here:
Search text in fields in every table of a MySQL database
(27 answers)
Closed 9 years ago.
I have many tables:
d_customers
d_customers
d_news
d_pages
d_products
d_projects
d_sms
and I want to create a search form to search for any word typed in it in all columns of all tables... But when write SQL code I see that it's long and confusing... Can any one tell me the right way to do this?
'SELECT * FROM d_customers,d_customers,d_news,d_pages,d_products,d_projects,d_sms
WHERE ' . $nc_make . 'LIKE .....
AND LIKE.... AND LIKE..... AND LIKE..... AND LIKE..... '
i want to search into all coulmns in all tables by LIKE word...if i search for google word i want to select all coumns in all tables where all columns like google
create table t1(a int);
create table t2(a int, b int);
insert into t1 values (1);
insert into t2 values (1,3);
SELECT *
FROM (
(select 't1' as tbl, a as Col1, null as Col2 from t1)
union
(select 't2' as tbl, a as Col1, b as Col2 from t2)
) as U
where U.Col1 = 1 or U.Col2 = 1
Result:
TBL COL1 COL2
t1 1 (null)
t2 1 3
If the tables are related to each other then join them and them apply your condition.
select *
from customers c
inner join pages p
on p.customer_id=c.customer_id
where customer_name like 'xyz'
You cannot avoid joins and conditions in the sql if they are necessary but you can optimize them.
If you want to generate query dynamically using programming and then want to execute then in mysql information_schema stores all the information related to the tables and the fields contained in that table. You can use it to generate your dynamic sql.
Hope this helps.
Related
This question already has an answer here:
Select values from a list that are not in a table
(1 answer)
Closed 2 years ago.
I have a list of values:
('WEQ7EW', 'QWE7YB', 'FRERH4', 'FEY4B', .....)
and the dist table with a dist_name column.
and I need to create SQL query which would return values from the list which don't exist in the dist_name column.
Yo need to use left join. This requires creating a derived table with the values you care about. Here is typical syntax:
select v.val
from (values ('WEQ7EW'), ('QWE7YB'), ('FRERH4'), ('FEY4B')
) v(val) left join
t
on t.col = v.val
where t.col is null;
Not all databases support the values() table constructor but allow allow some method for creating a derived table. In MySQL, this looks like:
select v.val
from (select'WEQ7EW' as val union all
select 'QWE7YB' as val union all
select 'FRERH4' as val union all
select 'FEY4B' as val
) v(val) left join
t
on t.col = v.val
where t.col is null;
You would typically put this list of values in a derived table, and then use not exists. In MySQL:
select v.dist_name
from (
select 'WEQ7EW' as dist_name
union all select 'QWE7YB'
union all ...
) v
where not exists (select 1 from dist d where d.dist_name = v.dist_name)
Or if you are running a very recent version (8.0.19 or higher), you can use the VALUES ROW() syntax:
select v.dist_name
from (values row('WEQ7EW'), row('QWE7YB'), ...) v(dist_name)
where not exists (select 1 from dist d where d.dist_name = v.dist_name)
SELECT TRIM(TRAILING ',' FROM result) result
FROM ( SELECT #tmp:=REPLACE(#tmp, CONCAT(words.word, ','), '') result
FROM words, (SELECT #tmp:='WEQ7EW,QWE7YB,FRERH4,FEY4B,') arg
) perform
ORDER BY LENGTH(result) LIMIT 1;
fiddle
The list of values to be cleared from existing values is provided as CSV string with final comma and without spaces before/after commas ('WEQ7EW,QWE7YB,FRERH4,FEY4B,' in shown code).
If CSV contains duplicate values all of them will be removed whereas non-removed duplicates won't be compacted. The relative arrangement of the values will stay unchanged.
Remember that this query performs full table scan, so it is not applicable to huge tables because it will be slow.
I'm working on rail data for different years (2009 to 2014) but just recently received data for two more years (2015 and 2016) separately. So my current tables are as follows:
Table1
YEAR....REV1....REV2....REV3....REV4....ORG....DEST
2009.....12......14.....15......16......ABC.....ABD
2010.....12......14.....15......16......ABC.....ABD
2011.....12......14.....15......16......ABC.....ABD
2012.....12......14.....15......16......ABC.....ABD
2013.....12......14.....15......16......ABC.....ABD
2014.....12......14.....15......16......ABC.....ABD
2009.....12......14.....15......16......XYZ.....XCV
2010.....12......14.....15......16......XYZ.....XCV
2011.....12......14.....15......16......XYZ.....XCV
2012.....12......14.....15......16......XYZ.....XCV
2013.....12......14.....15......16......XYZ.....XCV
2014.....12......14.....15......16......XYZ.....XCV
Note that the values 12,14,15,16 are just random number I wrote now, the value are different and differ from one year to the other. And the list go on giving rows for all the years for each different pair of ORG and DEST.
Table2 (2015)
YEAR....REV1....REV2....REV3....REV4....ORG....DEST
2015.....12......14.....15......16......ABC.....ABD
2015.....12......14.....15......16......XYZ.....XCV
2015.....12......14.....15......16......VRF.....AVC
2015.....12......14.....15......16......DRE.....ACS
2015.....12......14.....15......16......FRE.....AWD
Table3 (2016)
YEAR....REV1....REV2....REV3....REV4....ORG....DEST
2016.....12......14.....15......16......ABC.....ABD
2016.....12......14.....15......16......XYZ.....XCV
2016.....12......14.....15......16......VRF.....AVC
2016.....12......14.....15......16......DRE.....ACS
2016.....12......14.....15......16......FRE.....AWD
What i'd like to have is a table with all the years but adding only those that exists on the ORG AND DEST basis, as the new table (2015 and 2016) has data for a lot of other pairs of ORG and DEST that i'm not interested in but rather only those I have in my first table as below:
Table1
YEAR....REV1....REV2....REV3....REV4....ORG....DEST
2009.....12......14.....15......16......ABC.....ABD
2010.....12......14.....15......16......ABC.....ABD
2011.....12......14.....15......16......ABC.....ABD
2012.....12......14.....15......16......ABC.....ABD
2013.....12......14.....15......16......ABC.....ABD
2014.....12......14.....15......16......ABC.....ABD
2015 ....12......14.....15......16......ABC.....ABD
2016.....12......14.....15......16......ABC.....ABD
2009.....12......14.....15......16......XYZ.....XCV
2010.....12......14.....15......16......XYZ.....XCV
2011.....12......14.....15......16......XYZ.....XCV
2012.....12......14.....15......16......XYZ.....XCV
2013.....12......14.....15......16......XYZ.....XCV
2014.....12......14.....15......16......XYZ.....XCV
2015.....12......14.....15......16......XYZ.....XCV
2016.....12......14.....15......16......XYZ.....XCV
I'm relatively new to SQL, so been reading about inner join but it seems to add columns rather than the rows I want. Any ideas on how I might do this?
Thank you in advance.
+1 for your efforts. Join works on matching 2 different columns. What you want is union of different tables based on ORG and DEST conditions in where
SELECT * FROM Table1
UNION SELECT * FROM Table2 WHERE `ORG` LIKE 'XYZ' AND `DEST` LIKE 'XCV'
UNION SELECT * FROM Table3 WHERE `ORG` LIKE 'XYZ' AND `DEST` LIKE 'XCV'
in case you want repetitive data as well, use UNION ALL instead of union.
UNION Would make it, try something like below code
SELECT * FROM Table1
UNION
SELECT * FROM Table2 WHERE `ORG` LIKE 'XYZ' AND `DEST` LIKE 'XCV'
UNION
SELECT * FROM Table3 WHERE `ORG` LIKE 'XYZ' AND `DEST` LIKE 'XCV'
I have two tables/Datasets. 1st table consists of 3 rows and the 2nd table consists of 97 rows.
I need to merge both these datasets/tables, so i get a final output of 100 rows in a table/dataset.
I tried using, Inner Join, Full outer Join. But none are giving me the required result.
Please let me know, what Joins or Operations I can use using either SQL or Python Script.
Timestamp kW_System Anomaly score Alert indicator
2016-09-08T07:17:07Z 174.877105 1.455553 1
2016-09-13T09:32:07Z 175.462994 0.952738 1
2017-01-14T23:03:07Z 181.580188 1.057076 1
Timestamp kW_System Anomaly score Alert indicator
2016-09-14T21:33:07Z 0 1.056694 1
2016-09-14T22:03:07Z 0 1.226853 1
2016-09-14T22:33:07Z 0 1.265696 1
.
.
.
.
.
.
2016-12-17T05:48:07Z 2.10767 2.599405 1
2016-12-17T06:18:07Z 2.306138 1.370845 1
2016-12-17T07:18:07Z 2.089892 1.887742 1
You should use UNION operator (or UNION ALL if you don't want to remove duplicate row)
SELECT * FROM 1st table
UNION
SELECT * FROM 2nd table
Assuming you want them gathered into a new table, create the new table like so:
CREATE TABLE new_tbl LIKE orig_tbl1;
Then insert the data from your two tables:
INSERT INTO new_tbl SELECT * FROM orig_tbl1;
INSERT INTO new_tbl SELECT * FROM orig_tbl2;
If you want to collect all the rows in one of your existing tables, just adapt the insert statement like so:
INSERT INTO orig_tbl1 SELECT * FROM orig_tbl2;
This should work in MySQL. There will be very similar statements in most other database systems.
We can emulate it by doing a UNION of a left join and a right join, like this:
-- t1 left join t2
SELECT * FROM t1 LEFT JOIN t2
UNION ALL -- include duplicates
-- t1 right exclude join t2 (records found only in t2)
SELECT * FROM t1 RIGHT JOIN t2;
I have a list of ids, and I want to query a mysql table for ids not present in the table.
e.g.
list_of_ids = [1,2,4]
mysql table
id
1
3
5
6
..
Query should return [2,4] because those are the ids not in the table
since we cant view ur code i can only work on asumption
Try this anyway
SELECT id FROM list_of_ids
WHERE id NOT IN (SELECT id
FROM table)
I hope this helps
There is a horrible text-based hack:
SELECT
substr(result,2,length(result)-2) AS notmatched
FROM (
SELECT
#set:=replace(#set,concat(',',id,','),',') AS result
FROM (
select #set:=concat(',',
'1,2,4' -- your list here
,',')
) AS setinit,
tablename --Your tablename here
) AS innerview
ORDER BY LENGTH(result)
LIMIT 1;
If you represent your ids as a derived table, then you can do this directly in SQL:
select list.val
from (select 1 as val union all
select 2 union all
select 4
) list left outer join
t
on t.id = list.val
where t.id is null;
SQL doesn't really have a "list" type, so your question is ambiguous. If you mean a comma separated string, then a text hack might work. If you mean a table, then something like this might work. If you are constructing the SQL statement, I would advise you to go down this route, because it should be more efficient.
I have a table with a composite key composed of 2 columns, say Name and ID. I have some service that gets me the keys (name, id combination) of the rows to keep, the rest i need to delete. If it was with only 1 row , I could use
delete from table_name where name not in (list_of_valid_names)
but how do I make the query so that I can say something like
name not in (valid_names) and id not in(valid_ids)
// this wont work since they separately dont identity a unique record or will it?
Use mysql's special "multiple value" in syntax:
delete from table_name
where (name, id) not in (select name, id from some_table where some_condition);
If your list is a literal list, you can still use this approach:
delete from table_name
where (name, id) not in (select 'john', 1 union select 'sally', 2);
Actually, no I retract my comment about needing special juice or being stuck with (AND OR'ing all your options).
Since you have a list of values of what you want to retain, dump that into a temporary table. Then do a delete against the base table for what does not exist in the temporary table (left outer join). I suck at mysql syntax or I'd cobble together your query. Psuedocode is approximate
DELETE
B
FROM
BASE B
LEFT OUTER JOIN
#RETAIN R
ON R.key1 = B.key1
AND R.key2 = B.key
WHERE
R.key1 IS NULL
The NOT EXISTS version:
DELETE
b
FROM
BaseTable b
WHERE
NOT EXISTS
( SELECT
*
FROM
RetainTable r
WHERE
(r.key1, r.key2) = (b.key1, b.key2)
)