i have a little difficulty in understanding how to do some INSERT SELECT.
For instance i have two tables.
TABLE : users
id | name | gender
1 | John | m
2 | Mary | f
TABLE : website
fid | url | id
1 | www.desilva.biz | 2
2 | gidhelp.com | 4
Now let's say i want to add another query to the table website. I get two variables, lets say:
$user = John;
$site = "www.google.com";
i want to select the id of John from users table and insert it into website table in one statement.
How can i do it?
Assuming your variables are already escaped properly and are not subject to SQL injection:
INSERT
INTO website (url, fid)
SELECT $site, id
FROM users
WHERE name = $user
Related
I have 2 tables users and cached_users. Table users is updated and that's my active table. cached_users is my second table and I update that table every 15-20 min.
TABLE USERS
+---------------+---------------+---------------+------------------+
| id | name | username | email |
+---------------+---------------+---------------+------------------+
| 1 | Johne Doe | john.doe | johndoe#mail.com |
+---------------+---------------+---------------+------------------+
TABLE CACHED_USERS
+---------------+---------------+---------------+------------------+
| user_id | name | username | email |
+---------------+---------------+---------------+------------------+
| 1 | Johne Doe | john.doe | johndoe#mail.com |
+---------------+---------------+---------------+------------------+
MY QUESTIOS IS:
How can I check the differences between the two tables and update if something is changed? So I have multiple rows and loop trough all users rows and find matching cached_users row and update if needed. I have more column in cached_users table so I can't do "DELETE/INSERT" method. I need an SQL statement for this action. If you need more information, feel free to ask.
select * from users union select * from CACHED_USERS
if the count is same as users then there is no updation has occured if differs means
select p.id from (select * from users union select * from CACHED_USERS) p group by p.id having count(*)>1
id exist more than one means those records need to be updated.
I have two tables with the following structures (unnecessary columns trimmed out)
----------------- ---------------------
| mod_personnel | | mod_skills |
| | | |
| - prs_id | | - prs_id |
| - name | | - skl_id |
----------------- | |
---------------------
There may be 0 to many rows in the skills table for each prs_id
What I want is all the personnel records which do NOT have an associated skill record with skill_id 1.
In plain English "I want all the people who do not have the skill x".
Currently, I have only been able to do it with the following nested select. But I am hoping to find a faster way.
SELECT * FROM `mod_personnel` WHERE `prs_id` NOT IN (
SELECT `prs_id` FROM `mod_skills` WHERE `skl_id` = 1 )
This may be faster:
SELECT `mod_personnel`.*
FROM `mod_personnel`
left outer join `mod_skills`
on `mod_skills`.`prs_id` = `mod_personnel`.`prs_id`
and `mod_skills`.`skl_id` = 1
WHERE `mod_skills`.`prs_id` is null;
Using a NOT EXISTS might be faster.
SELECT *
FROM `mod_personnel` p
WHERE NOT EXISTS (SELECT *
FROM `mod_skills` s
WHERE s.`prs_id` = p.`prs_id`
AND s.`skl_id` = 1 );
I have a user table and a contact info table so that each user can have multiple phone numbers and multiple email addresses. Is there a standard way to set and retrieve their primary contact info?
"humans" table
ID | name
1 | John
2 | Joan
"human_contact" table
ID | contact_type | contact_info | user_id
1 | email | john#a.com | 1
2 | email | johnny#b.net | 1
"tickets" table
ID | human_id | event_date
1 | 1 | 2017-08-01
if John usually uses johnny#b.net, how do I mark that row as his favorite and retrieve that one for most queries?
SELECT *
FROM tickets
LEFT JOIN humans
ON tickets.human_id=humans.ID
LEFT JOIN human_contact
ON human_contact.human_id=humans.ID
WHERE tickets.ID='112'
You can use an extra coloumn named as fav which will have boolean values. This can help you in first finding whether he has any fav using a check where fav==true, then going on with that contact_info , or if the user doesnt have any favourites, then go with your usual query.
That is,
if( query( fav==1 for atleast 1 tuple )){
return tuple with fav==1;
else{
query ( usual query to get contact_info );
return query_result;
}
for more easier understanding.Hope this helps.
I have two tables:
users (user_id, user_name, user_email, user_pass)
&
toys (name, box)
I need each user to have their own toys table. Basically the table for users is for my login form and the toys is where the user choose the toys once they login.
Add an extra table that have FK (foreign keys) to both Users and Toys, that acts as the table for the many-to-many relation
i.e. create a table called user_toys that has toy_id and user_id combination per row. Then if you want to get all the toy names for a particular user, you can just do
SELECT t.name
FROM Toys t,
User_toys relation,
WHERE relation.toy_id = t.toy_id
AND relation.user_id = :user_id
(Note, you don't need to join to the Users table if you already have the user_id in the relation table)
If each toy can only belong to one user (1-to-many), then adding an extra column for user_id FK on the Toys table would suffice. But sounds like from your question each user have their independent set of toys to pick.
reference: https://en.wikipedia.org/wiki/Associative_entity
hello you need one field in table Toy to make your table become relation, here is my schema based on yours
here is toys
+--------+-------------+--------------+-----+
| id_box | id_box_user | id name | box |
+--------+-------------+--------------+-----+
| 1 | 1 | Name toys 1 | box |
| 2 | 2 | Name toys 3 | box |
+--------+-------------+--------------+-----+
then this is user
+---------+-------------+------------+-----------+
| user_id | user_name | user_email | user_pass |
+---------+-------------+------------+-----------+
| 1 | user name 1 | email | pass |
| 2 | user name 2 | email | pass |
+---------+-------------+------------+-----------+
and you need query like this to get all data with spesific user
SELECT * FROM user a, toys b where a.user_id=b.id_box_user_id
but for spesific user use this
SELECT * FROM user a, toys b where a.user_id=b.id_box_user_id and a.user_id='variable which store session'
and this is the screen shot
I have some question : "insert data in different table but have same id (auto_increment)"
This is my twice table
student
---------------------- id | name | class|
----------------------
score
--------------------------------------------- id | math | english | physical | year |
---------------------------------------------
I have some data like:
John , A , 90 ,80 ,70 ,2015
John , A , 70 ,90 ,50 ,2016
I want result Like:
student
---------------------- id | name | class|
---------------------- 1 | John | A |
----------------------
score
--------------------------------------------- id | math | english | physical | year |
---------------------------------------------
1 | 90 | 80 | 70 |2015 1 | 70 | 90 | 50 |2016
Because table of student's "id" use Auto_increment
If I want twice table have same id when I insert data
What SQL can I use ??
(I think mysql_insert_id() is Solution but not sure...)
Thank help !!
I hope you are looking for the query to do so.First of all use PDO or mysqli for interacting with the database.Here I use PDO.
function my_select($query)
{
global $dbserver,$dbuser,$dbpwd,$dbname;
$dbh= new PDO('mysql:host='.$dbserver.';dbname='.$dbname.'',$dbuser,$dbpwd);
$rs=$dbh->prepare($query); //prepared statements have numerous advantages over executing sql statements directly
$rs->execute();
return $db->lastInsertId(); //returns last inserted id of current db connection
}
And the query can be,
$l_id=my_select(query1) // query to insert into student
$query2="INSERT INTO score (id,...) VALUES ('".$l_id."',...)" ;
my_select(query2);
If you are using Auto Increament id in both tables than you need to add a column in score table for reference of studentid. For example:
Insert into student (name) values ('test');
Than use:
mysql_insert_id();
In last insert data into score table:
Insert into score (yourcolumns, studentid) values (yourcolumnvalue, last_inserted_id);
Side note: instead of mysql_*
functions use mysqli_* or PDO becuase mysql_* is deprecated and not available in PHP 7.