delete item that are not present on another database - php

I tried this code to delete the item that not present on another database what should i do ?
mysqli_query($link,"delete from payment where INVOICE_NO_MX NOT IN(select * from invoice) ");

You should be comparing INVOICE_NO_MX against the correct column in invoice. Something like this:
DELETE
FROM payment
WHERE INVOICE_NO_MX NOT IN (SELECT INVOICE_NO_MX FROM invoice)
^^^ replace with appropriate column name

Please replace * with appropriate field name.
select '*' from invoice.
This query returns multiple columns but in sub-query you can return only 1 column or summary functions.
Hope this will solve your problem.

Related

How to compare the one table elements to another table

Using below query I am getting the last record which is updated in the table using update_date
SELECT form_id,form_elements,form_builder_type, update_date FROM `form_builder` WHERE form_id=1 AND form_builder_type='example' ORDER BY update_date DESC LIMIT 1
Output is below
Now I have the second table with field name and field type
I have to check the form_elements which are coming from the first table is available or not in the second table.
If available in the table then display the field name and field type
I need an output like
lastname: text
email : email
Would you help me out in this?
will not tell you that saving data like you do is not correct way to do things, so:
SELECT t.id, t.form_builder_type, e.field_type FROM form_builder t
LEFT JOIN form_elements e
ON e.field_name regexp concat('(',replace(e.form_elements, ',','|'),')');
Hope you'll use it to update your scheme ;)

How do i get record by id in php

Here is my database view
I need to get title by using student id 232. But student id is stored in array like 192,229,232. How can i retrive 232 records only?
Please guide me..
Thanks
you use IN clause for get record from array
<?php
$sql = 'SELECT *
FROM `table`
WHERE `id` IN (' . implode(',', array_map('232', $array)) . ')';
Use FIND_IN_SET
Eg.
SELECT * FROM event WHERE FIND_IN_SET(232,student);
You can use LIKE condition to fetch results that contain this student id:
SELECT * FROM `event` WHERE student LIKE '%232%';
This will return you all rows that contain '232' in student column.
But if you have an opportunity to change your table structure I would suggest you to extract student column to a pivot table.
You could create event_student table that would contain just two columns: event and student. And each row would link particular student to a particular event. The data could be easily extracted with a simple JOIN:
SELECT * FROM `event` e
INNER JOIN `event_student` es ON e.eventID=es.event
WHERE es.student=232;
If you store connections between student and events this way you will gain flexibility in your queries and avoid possible mistakes that may occur when using the answer suggested above. Imagine that you have a student with id 2324 or 12323. Both of them contain desired '232' string and both will match the '%232%' pattern and lead to returning you wrong data.

Adding database entries together

I am making a site which allows admins to basically add points for a user.
At this point in time, I have a table, where id_child is unique, and id_points changes. So a constant stream of id_points can come in, however, it will only show the latest id_points, not the total.
I am wondering how I could go about creating a PHP script that could add all of those together.
From the image, the idea is that I want all id_points values added together to give a total, and this is for the same id_child
Use SQL sum() funciton:
select sum(id_points) from table `table_name` where `id_child` = 1
Hope i understood right.
First if you want to show only the latest points added you have to create another table #__points where you will keep every new change of points.
You need 3 columns id as PRIMARY and AUTO_INCRENMENT , pts and user_id . user_id will be FK to id_child.
So when you want to add a new record :
INSERT INTO `#__points` (pts,user_id) VALUES ("$pts",$id)
When you want to select last inserted value for each admin :
SELECT * from `#__points` where user_id=$id ORDER BY id ASC LIMIT 1

Discover last insert ID of autoincrement column in MySQL

I'm currently using:
SELECT MAX(id) FROM table
To discover the current id of a certain table, but i heard this can bring bad results. What is the proper way of doing that? Please, notice that i'm not INSERTING or DELETING anything before that query. I just want to know the current ID, without prior INSERT or DELETE.
Perform the following SQL:
SHOW TABLE STATUS LIKE 'TABLENAME'
Then check field AUTO_INCREMENT
You can use the following query:
SELECT id FROM table ORDER BY id DESC LIMIT 1;

How get incremented value from table

I need to get next id from table (auto_increment).
I could just use SELECT * from table ORDER BY id DESC LIMIT 1;
For example I get 50. But if we delete from table two items I will get 48 but correct one
will be 51. How get correct value even we something delete from table ?
You can only use SHOW TABLE STATUS LIKE 'tablename' to fetch the auto_increment value. A simpler solution might be: SELECT MAX(id) + 1 FROM table, but this is buggy if the last entry was deleted.
show table status like 'table_name'
next id value is in 'Auto_increment' field
SHOW TABLE STATUS LIKE 'table'
The value you want is in the Auto_increment field.
Be careful about concurrency though: by the time you get around to using this value, some other client could have inserted into the table and thus your value is out of date. It's usually best to try to not need this.
SELECT LAST_INSERT_ID() + 1;
gets the last ID used in an insert in an autoincrement column + 1
I see two solutions for the next ID:
1) Select bigger value of a column with max function. Example: select max( id ) from table;
2) Using the command SHOW STATUS LIKE and get the correct index of array. Take a look: http://dev.mysql.com/doc/refman/5.1/en/show-table-status.html
Seems to me you're creating a race condition here.
Why exactly can you not insert the row you want to insert and then use LAST_INSERT_ID() to find it's ID?

Categories