Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    MYSQL




Delete Rows From Table A, Which Dont Exist In Table B (base On Column X)


I have 2 tables identically structured.

A & B

Table A, has column: Product (product code) as primary key

Table B doesn't.

Apart from that they have the exact same fields.

There's also a column: supplier

I want to

Delete * from table A, where does not exist in B (based on column: Product) & where supplier = apples

So to elaborate. Table A is my main table, but it now contains outdated products from supplier apple. Table B has the latest list of products from supplier apple. So I want to remove old products from A that supplier apple no longer makes.

mysql version 4.0.27




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Delete If Table Exist
I would like to delete rows only if table exist to prevents errors.

Also I would like to add column only if it doesn't already exist.

Selecting Rows From One Table With A Key That Doesn't Exist In Another.
I'm trying to select rows from one table that does not have a key in another table.

For instance:

I have a hospital table with two columns, id and name.

I have a patients table with three columns, id, name, and locationid.

Now I want to find those hospitals that have no patients. So I want to get those hospitals whos id does not equal any of the locationid's in the patients table.

If Record Exist In 2 Tables, Use Column Of Table B
I have 2 tables A and B...
Table A: A_id, field1, field2, ..., status
Table B: B_id, A_id,..., status

I need to select all info from table A. And if a record exist in table B, i need to use the status value of that table instead of the one in table A.

Im not sure how to do it...with EXISTS or CASE?

Delete The First Rows In A Table
How i can do to delete all rows in a table less the last three?
ex.
id name
1 cccf
2 ccce
3 cccd
4 cccc
5 cccb
6 ccca

The query delete all row less
4 cccc
5 cccb
6 ccca

Delete Everything From Table Except The Top 100 Rows
Does anyone know the mysql syntax to delete everything in a table except the top 100 rows?

Do you need to use LIMIT?

Update A Record Out Of 2 Duplicate Records Which Dont Have Primary Key In Mysql Table
how to update a record out of 2 duplicate records which dont have primary key in mysql table?

Two Table Query: Grab Rows From One Table Even If No Related Row In Other Table
PHP

$gettray = mysql_query("SELECT trailers.title,
trailers.link,
trailers.movie,
movie.title AS mtitle
FROM trailers,movie
WHERE trailers.movie=movie.word
ORDER BY trailerid
DESC LIMIT 6",$connm);

It works great, but there is one problem. It will not grab any rows from the 'trailers' table if a corresponding movie row does not exist in the 'movies' table.

I want it to pull ALL rows from the 'trailers' table, even if the corresponding row in the 'movies' table does not exist yet.

If the row does not exist in 'movies', the program than uses the entire trailer title like so


PHP

if($ttray['mtitle']) {
  $newttitle = explode("-",$ttray['title']);
$newttitle = array_reverse($newttitle);
$ttitle = $newttitle[0];
$ttitle = $ttray['mtitle'] ."- ". $ttitle;
} else {
$ttitle = $ttray['title'];
}



Thanks
Ryan

Delete From Multiple Tables Base On 2 Values..
.. the title pretty much says it all. My experimenting led to two of my tables being dropped so, i'm looking for some help!

i need to delete from 3 tables but only the row in each table that has the user column and pic column equal to the values i pass it. I hope my problem description is clear enough.. let me know if it's not!

Does The Table Exist
iam developing a servlet for accessing a database (iam using MySql database),
and iam wondering, how do i know if i have a certain table in thr database or not!!!
i mean how can i check if i have a certain table in the database!!! abf if dont have it, what is the error msg that i receive, so i can handel the situation !

Table Doesn't Exist ERROR
I'm getting a "Table doesn't exist" error. The name of the table is "sitesearch"
But the table does exist! Strange...

So I executed :

mysql
SHOW TABLES

...and used PHP var_dump() to output the array fetched. The var_dump() gave me this :

Selecting Ids From One Table That Don't Exist In Another
I have 2 tables, they are both linked together and my life would be so much easier if they were just one big table, but they're not They have a one-to-one relationship between them and I need to find out the records in table 1 which don't have corressponding values in table 2. How can I do this?

SELECT id FROM table1 WHERE id NOT IN table2
Will that do it?


1146 Table Db.1 Does Not Exist..
Create a table in MySQL v5 using MySQL Control Center. When I try to open it, it opens OK but complains that a table (db.1) does not exist. I KNOW it doesn't exist. I DON'T WANT it to exist. I frankly couldn't care too hoots about not finding a table that I don't want and doesn't exist, so WHY must MySQL insist on telling me a non-existant table doesn't exist?

Temporary Table Does Not Exist
I've had this code running for over a year and now, all of a sudden, it no longer works.
I'm using PHP and MySQL 3.23.58 which my host provides. Code:

Does Table Exist Code?
I ran across this code to check if a table exists. I'm thinking there has to be a better way...maybe something like DoesTableExist("MyTable") = true or false.

Comments?

global $exists;
$exists = 0;
$dataset = mysql_query('SHOW TABLES') ;
while( $row = mysql_fetch_row($dataset) )
{
if ( $row[0] == "mos_tbd_tags" ) {
$exists = 1;
break;
}
}

Insert No Duplicates And Only If Not Already Exist In Other Table
is there a way to combine these two querie in one query?

I want to do this both:

SELECT *
FROM inventimport WHERE PC_volgnr NOT IN (SELECT dev_volgnr FROM dev) AND PC_volgnr <> 'geen pc aan' Order by inv_id

AND

SELECT DISTINCT PC_volgnr
FROM inventimport

Insert Values Only If They Do Not Already Exist In Table
I want to insert some values from one table (A) into another table (b). However there are some values in table A which exist two times in table A. I only want to insert values one time. So If a certain field value also exist in some other row (same fieldname) then I don't want to insert it twice. It doesn't matter which of the two rows will be inserted.

Can this be done with MySQL? Maybe with some Control Flow Functions?

Error 1146 Table Doesn't Exist
I made a backup of my db with 11 tables. I made some tests with the original tables, like delete data from some. After the tests I copied and pasted the backup to the original place. Now when I command: use mydatabase; Mysql goes ok. When I command: show tables; Mysql shows me the eleven tables in the db. But when I command:

select * from my table; Mysql returns to me the following message ERROR 1146 (42S02):

Table 'mydatabase.mytable' doesn't exist
What can I do to fix the problem?

Select Timestamp That Doesn't Exist In Other Table
Some of you probably know this trick to select rows from one table with an ID value that doesn't exist in another table:

mysql> SELECT table1.* FROM table1
-> LEFT JOIN table2 ON table1.id=table2.id
-> WHERE table2.id IS NULL;

But now take look at this particular situation:

CREATE TABLE `table1` (
`id` int(11) NOT NULL auto_increment,
`data` varchar(255) NOT NULL default '',
`created` timestamp(14),
PRIMARY KEY (`id`)
);

Same goes for table 2.
Now this is my problem: how can i select rows from table 1 with a timestamp `created` that doesn't exist in table 2? the IS NULL trick doesn't seem to work. I guess that is because a timestamp can never be null.

Table Doesn't Exist After Renaming Database
I have renamed my database from songsdb to songs by renaming the folder under MySQLMySQL Server 5.0datasongsdb to songs (stopped the server before doing so). Now when I restart, I see songs database, show tables shows all tables, but desc command gives error: ERROR 1146 (42S02): Table 'songs.djs' doesn't exist!

Whatelse am I to change to recognize these tables? I'm using 5.0.16-nt version on Windows XP.

Only Showing Records That Do Not Exist In Another Table
How do I show a listing of all records within one table that do not exist within a second table?

For example:

Table A
----------
apple
orange
pineapple
banana

Table B
----------
orange
cherry
lime
banana

I want all records in A that do not exist within B to show. thus I should have the results of:

apple
pineapple

What would my sql statement be if my column name was "fruit"?

Error 1146: Table Does Not Exist
OS XP Pro SP2
MYSQL Ver: 5.0.0-alpha nt

Whenever I view a table or run a query in the Control Center (0.9.4-beta) I
get the following error:

[ShipNet] ERROR 1146: Table 'shipnet.1' doesn't exist

table shipnet.1 indeed does not exist but I can't understand why it's
looking for it. Any ideas?

FYI coincidently shipnet1 is the name of my computer.

Table 'mysql.host' Doesn't Exist
Working with mySQL 3.23.58 on Linux, I get this error when trying to start mysqld:
--------------------
060716 03:07:21  mysqld started
060716 03:07:21  /usr/local/mysql/libexec/mysqld: Table 'mysql.host' doesn't exist
060716 03:07:21  mysqld ended
---------------------

This Linux machine has been working ok for months, and it stopped suddenly.

I don't've any idea about how to fix it, and I don't find any '/etc/my.conf' file
into this machine. The install directory is '/usr/local/mysql'.

Table 'mysql.host' Doesn't Exist
Working with mySQL 3.23.58 on Linux, I get this error when trying to start mysqld:
--------------------
060716 03:07:21 mysqld started
060716 03:07:21 /usr/local/mysql/libexec/mysqld: Table 'mysql.host' doesn't exist
060716 03:07:21 mysqld ended
---------------------

This Linux machine has been working ok for months, and it stopped suddenly.

I don't've any idea about how to fix it, and I don't find any '/etc/my.conf' file
into this machine. The install directory is '/usr/local/mysql'.

Table 'test.event' Doesn't Exist
I'm evaluating MySQL 5.0.37-community-nt MySQL Community Edition (GPL).
For some reason I get this error. I don't recall doing anything in particular to cause this to happen. I can see the table in with the show tables; command. However, I can't select from it, insert into it or drop the table.
Can anyone tell me what caused this and how to correct it?

Using Table A Column Row Data For Table B Column Headings
Is it possible to add column data in table A and have (dynamically
linked) table B column headers?

Using Table A Column Row Data For Table B Column Headings
Is it possible to add column data in table A and have (dynamically
linked) table B column headers?

Error 1146 - Table Mysql.1 Does Not Exist
I installed Mysql 5.x fine no problem and everything works , except when I try to do table mgmt in the Mysql control centre, eg delete rows from a table using the mouse. I invariably get this

error: 1146 Table 'mysql.1' does not exist

I think '1' is a temporary table that some setting is preventing me from using. Does anyone know what to change?

#1146 - Table 'inhous1_demo1.Admins' Doesn't Exist
i have table 'admins' in database. when i execute query like----->

-->SELECT *FROM `admins`;
i got result.
but
when i write query like---->
---> SELECT *FROM `Admins`;
i got error #1146 - Table 'inhous1_demo1.Admins' doesn't exist .

Mysql Table Doed Not Exist Problem
I am trying to "select" from a mysql table called "quiz".

I can execute the query successfully from command prompt.
select * from quiz;

but using JDBC, I am getting error
Table 'mysql.quiz' doesn't exist

Can anyone tell me why it is accessing "mysql.quiz" instead of "quiz".
what should i do now to tell mysql to look into quiz instead of "mysql.quiz".

ALTER TABLE Deleted The Rows - Error: Table Is Full
Today when I tried to insert data in a table i received the error that 'table is full'. On SHOW TABLE STATUS, I noticed the size of table is grown to 4GB (rows 5359211).

mysql> SHOW TABLE STATUS LIKE 'messages' G
*************************** 1. row ***************************
Name: messages
Engine: MyISAM
Version: 9
Row_format: Dynamic
Rows: 5359211
Avg_row_length: 801
Data_length: 4294967288
Max_data_length: 4294967295
Index_length: 45783040
Data_free: 0
Auto_increment: 5406252
Create_time: 2007-04-20 18:26:38
Update_time: 2007-08-22 09:55:22
Check_time: NULL
Collation: latin1_swedish_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)
Here is the table structure.


CREATE TABLE `messages` (
`id` int(11) NOT NULL auto_increment,
`subject` varchar(255) NOT NULL default '',
`message` text NOT NULL,
`attachment_path` varchar(255) default NULL,
`new` tinyint(4) NOT NULL default &#390;',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

On searching I found the this link.
http://dev.mysql.com/doc/refman/4.1/en/full-table.html

According to manual i executed the following query

MySQL
ALTER TABLE messages MAX_ROWS=20000000000;
I skipped AVG_ROW_LENGTH from the query, I was confused what should be the value for this.

After executing the query, when i check the table status it displayed totally different picture. Now there were only 170109 rows left.


mysql> SHOW TABLE STATUS LIKE 'messages' G
*************************** 1. row ***************************
Name: messages
Engine: MyISAM
Version: 9
Row_format: Dynamic
Rows: 170109
Avg_row_length: 649
Data_length: 110563036
Max_data_length: 281474976710655
Index_length: 1961984
Data_free: 0
Auto_increment: 5409214
Create_time: 2007-08-23 10:07:08
Update_time: 2007-08-23 13:41:57
Check_time: NULL
Collation: latin1_swedish_ci
Checksum: NULL
Create_options: max_rows=4294967295
Comment:
1 row in set (0.00 sec)
What could be the reason of this rows deletion?

I am on 32 bit system.
MySQL version: 4.1.18-standard-log
Operating System : CentOS 3.x
Memory: 4 GB DDR

Updating Rows In Table B Based On Related Field In Table A
Ver 4.1.8-standard for apple-darwin7.6.0 on powerpc (Official MySQL-standard binary)

I am trying to do some data migration based. I have several tables that contain our legacy pkey field and I want to update the tables with new ID's.
I need to do this several times and have tried it several ways to no avail.

Table A
---------
companyID int(10) pKey
legacyID int(10) old legacy pkey

Table B
---------
bAID int(10) pkey
companyID int(10)
legacyID int(10)

Table A has values for both companyID (unique key) and legacyID.
Table B has values for bAID (unique key) and legacyID but companyID is empty.

I need to update tableB.companyID with tableA.companyID based on tableb.cSerialID to tablea.cSerialID relationship.

I need a query that will update ALL rows.

Copying Column Names And Properties From An Existing Table To A New Table?
I've got a table with 42 columns, set to different types (string, datetime, date, integer). It has 8 million rows. I want to make a new table with the exact same columns, but with no data in it. Is there an easy way to do this without re-defining every column?

ERROR 1146 (42S02): Table 'spnau_dev.spde_user' Doesn't Exist
I used the migration tools try to migrate tables. There are a table can't be create because of the constraint. I fixed it and create the table under the schema and then continue the migration. After migration completed and when I select, drop the table, I got this error. when I drop the schema, I also got the error.

ERROR 1146 (42S02): Table 'spnau_dev.spde_user' doesn't exist

This table is innodb type. we are using version 5.0.27-standard.

when I show table in the schema, I can see the table.

in the information_schema.table. I saw the table. But the information is all not right.

'', 'spnau_dev', 'spde_user', 'BASE TABLE', '', , '', , , , , , , , '', '', '', '', , '', 'Table 'spnau_dev.spde_user' doesn't exist'

Can someone help me on this ?

Delete File If Exist
I often create excel file from mysql in my local windows machine. I use

select last_name from mytable into outfile 'temp.xls';

the 1st time run is ok. the 2nd time run, it says file is already exist. how to write a script to find if the file exsit, then delete and run my select to create a new excel file?

1036:Table Read Only Error, Cant Add A New Column To My Table
I have a table with arround 30 columns, and I am trying to add another column to it with the following query.

alter table tablename add column column_name varchar(10);

and I get the following error.
ERROR 1036: Table 'ProfilesDesc' is read only

what could be wrong?

Help With 3 Table Query &amp; Counting Rows In Third Table
I have three tables, a members, a vendors and a products. I'm using a query to grab all of the rows from the first two tables, matching on a primary key. This is easy and works fine. However, I'm trying to pull the number of products from the third table and it's giving me trouble. Basically, I can count the products for each vendor if products exist, but if there are no products, instead of returning 0, no rows are returning.

current query looks like this:

SELECT t1.*, t2.*, count(*) AS count FROM vendors as t1, members AS t2, products AS t3 WHERE t2.mem_id = t1.mem_id AND t3.vendors_id = t1.vendors_id GROUP BY t1.vendors_id;

So, all of the vendors that have zero products are being left out of the results. Does anyone know if it's possible to achieve this without using a temp table?

Fatal Error: Can't Open Privilege Tables: Table 'mysql.host' Doesn't Exist
When I start the MySQL server it gets stopped immediately logging the following error.
050607 15:27:43 InnoDB: Started
050607 15:27:43 Fatal error: Can't open privilege tables: Table 'mysql.host' doesn't exist
050607 15:27:43 Aborting

050607 15:27:43 InnoDB: Starting shutdown...
050607 15:27:45 InnoDB: Shutdown completed

Update Column In Table A With The Count Of Another Table
Having my SQL-background in Oracle I'm looking for the MySQL equivalent for:

update tableA
set column1=(select count(*) from tableB where tableA.key= tableB.key)

cannot find anything similar in the manual, but probably overlooking
something.

Create Table / Column Table Order
When creating a table, is there any advantage in specifying the columns in
any special order? For example, what about columns with data used more
frequently listed first, or column/data types giving some? Or.... am I being
pedantic even thinking about such?

Match Rows In Table B With Rows In Table A
How do I structure a query to match rows in table B with rows in table A where column in B contains strings that contain data from column in A..

Example:

B.part = "abcdefg" matches A.part ="cde"

I cannot put literal in query.. it must be from column data..

It's easy to match rows where columns are equal, but I can't figure out how to get a match with "substring" as shown.

Deleting Rows In One Table That Don't Have A Corresponding Row In Another Table.
I have two tables:

Table: Users
UserName (primary key)
Password
PurgeDate

Table: Logins
UserName
LoginDate

I want to do two things:

1) Delete all records from the Logins table where the value of UserName cannot be found in the UserName column in the Users table.

2) Records are purged from the Users table based on the PurgeDate. When that happens, I want to purge all records from the Logins table where UserName has the same value as the value in UserName in any record purged from the Users table.

Rows Of A Table As Columns Of Another Table
with the following tables i have the problem written below:

table group
gr_id, name

table gr2kr
id, gr_id, kr:id

table krit
kr_id, name

now i don't want to get an output like

name1 krit1
name1 krit2
name2 krit1

i want something like this
name1 krit1 krit2
name2 krit1

does anybody have an idea how or if it is possible?

Copy Rows From One Table To Another Table
One is the "main" machine and all the others will synch data up with it it once in a while. Is there a command to insert rows into one table on a different machine using the table on the current machine? Like....

Main Table 1 on main machine e.x. 192.168.1.1
name|email|listdate
------------------------
tom|a@b.com|1-1-2005
dick|c@d.com|1-1-2005
harry|e@f.com|1-1-2005
etc.



Temp Table 1 on field machine e.x. 192.168.1.2
name|email|listdate
-----------------------
larry|g@j.com|6-1-2005
mike|h@k.com|3-1-2005
fred|i@l.com|5-1-2005


i know you can do something like the following if they are both located on the same machine but i don't know how to structure the command for different machines:

insert into real_table (col1, col2) select x, y from temp_table;


Incrementing Rows That May Not Exist
I'm sure there's a very simple solution to this, I just haven't been able to find one I understand yet.

I have a table with 3 columns, the first two are used as an index, and the third is a counter. So far so good:

Joins To Rows That Don't Exist
I want to join two tables together, but if the secondary table doesn't have a row that matches, I want the primary table's row to still be returned, with the values for the secondary row being their default values. How do I accomplish that?

For example:

if I have two tables:

t1: defaults:(a=0,b=0)
+---+---+
| a | b |
+---+---+
| 1 | 3 |
| 2 | 4 |
+---+---+

t2: defaults:(a=0,c=0)
+---+---+
| a | c |
+---+---+
| 1 | 7 |
| 3 | 8 |
+---+---+

I want to execute a query like:
SELECT t1.a,t1.b,t2.c FROM t1 JOIN t2 USING(a)

But I want the result set to be
+---+---+---+
| a | b | c |
+---+---+---+
| 1 | 3 | 7 |
| 2 | 4 | 0 |
+---+---+---+

Can A Composite Key Exist As A New Column?
I am trying to create a composite key in a table with two fields but I want to create a separate column which should be the combination of the 2 fields and should have some name to be worked with. Is this possible?

My query:

Why Does The Slow Query Log Show More Rows Than Exist?
# Time: 070528 17:14:57
# User@Host: counter[counter] @ localhost []
# Query_time: 3 Lock_time: 0 Rows_sent: 7 Rows_examined: 120647
SELECT SQL_CACHE `webpageUrl`, `webpageName`, COUNT(*) AS `count`, (COUNT(*) / (SELECT COUNT(*) FROM _1_log)) AS `pct` FROM _1_log GROUP BY `webpageUrl` ORDER BY `count` DESC LIMIT 7;

mysql> select count(*) from _1_log;
+----------+
| count(*) |
+----------+
| 111824 |
+----------+
1 row in set (0.00 sec)

Delete A Table
i have noticed that there are loads of unwanted table in my mysql database and i need to completely delete those tables..but i find only two options through myphp admin...empty and drop..
but i need to delete the whole table ..how can i do it??

Delete From Two Table
I have two separate tables - ADDRESS and CUSTOMER I want to delete all the ROWS under the table ADDRESS, WHERE CUSTOMER CUSTOMER_ID=1

(CUSTOMER_ID is located in the CUSTOMERS table.)


Copyright © 2005-08 www.BigResource.com, All rights reserved