How to insert into table based on other one? - mysql

I have these two tables:
// users
+----+-------+-----------------------+--------+
| id | name | email | active |
+----+-------+-----------------------+--------+
| 1 | peter | peter12#hotmail.com | NULL |
| 2 | jack | most_wanted#gmail.com | NULL |
| 3 | john | john_20016#yahoo.com | NULL |
+----+-------+-----------------------+--------+
// activate
+----+---------+---------------------+
| id | post_id | random_string |
+----+---------+---------------------+
| 1 | 2 | fewklw23523kf |
+----+---------+---------------------+
Also I have these two variables:
$random string = 'gergflkw4534l';
$email = 'peter12#hotmail.com';
And I want to insert a new row into activate table based on those two variables. Here is expected result:
// activate
+----+---------+---------------------+
| id | post_id | random_string |
+----+---------+---------------------+
| 1 | 2 | fewklw23523kf |
| 2 | 1 | gergflkw4534l |
+----+---------+---------------------+
As you see, I get post_id from users table based on email column. How can I do that?
INSERT INTO activate VALUES (null, /* I don't know */, :random_string)
INNER JOIN users ON /* I don't know */
WHERE email = :email

Your syntax is very much off. You need a WHERE clause before you can actually JOIN anything to it. I'm going to assume that the id column in the Activate table is an autonumber column.
INSERT INTO Activate (post_id, random_string)
SELECT
id,
:random_string
FROM
Users
WHERE
email = :email

You probably want to use the mysql insert-select syntax:
INSERT INTO activate (post_id, random_string)
SELECT id, :random_string
FROM users
WHERE email = :email
LIMIT 1;

Related

How can I insert data to table B with more columns than table A with the same ID (ID matches from both tables)?

So here is my problem, how can i do this :
user with ID = 7 from table A with 3 columns want to insert data to table B with 4 columns but with the same id.
Table A :
| Id | name | password |
| 7 | john | password |
| 9 | mark | password |
| 12 | yuta | password |
Table B :
| Id | user_id | food | drink |
| 1 | 7 | oats | milk |
| 2 | 9 | fish | water |
| 3 | 12 | pear | fanta |
How can i achieve table b in 1 query? both id in both table are primary keys and im using mysql
here's the code i was trying to do :
INSERT INTO table_b SET food = :food, drink = :drink, ( user_id) SELECT a.id FROM table_a u WHERE EXISTS (SELECT * WHERE name = :name AND password = :password)
i know the query is wrong but thats the closest i can do. pls help thank you
You should just use a unique code to link them together.
you can have the code stored in a variable like this
$unique = time().rand(1000, 9999);
//You should have something like this 16659154332358
Create a column that will recieve this value in your database.
Now they have a relationship

Joining three tables to get a list of tags

I have these three tables:
user_submitted_value
id | owner_id | value |
-----------------------
1 | 1 | 1337 |
2 | 2 | 1337 |
3 | 2 | 1337 |
4 | 1 | 1337 |
tag
id | owner_id | text |
---------------------------
1 | 1 | 'Tag 01' |
2 | 1 | 'Tag 02' |
3 | 1 | 'Tag 03' |
4 | 2 | 'Tag 04' |
user_submitted_value_tag
id | owner_id | tag_id | value_id |
-----------------------------------
1 | 1 | 1 | 1 |
2 | 1 | 2 | 1 |
3 | 1 | 3 | 1 |
So basically, users can submit values and enter any number of freetext tags to attach to that value. I need to store the tags as belonging to a specific user, and I need to be able to count how many times they've used each tag.
What I want to accomplish is a query that gets rows from user_submitted_value with the tags appended onto them. For example:
Query value with id 1:
id | owner_id | value | tags |
------------------------------------------------------
1 | 1 | 1337 | "'Tag 01','Tag 02','Tag 03'" |
Query all values belonging to user with id 1:
id | owner_id | value | tags |
------------------------------------------------------
1 | 1 | 1337 | "'Tag 01','Tag 02','Tag 03'" |
4 | 1 | 1337 | "" |
I know I need to JOIN one or more times, somehow, but I am not comfortable enough with SQL to figure out exactly how.
This seems like a rather arcane data format -- particularly because owner_id is repeated in all the tables.
In any case, I think the basic query that you want to get the values and tags for a given user looks like this:
select usv.owner_id,
group_concat(distinct usvt.value_id) as values,
group_concat(distinct t.text) as tags
from user_submitted_value usv join
user_submitted_value_tag usvt
on usv.value_id = usvt.value_id and usv.owner_id = usvt.owner_id join
tags t
on usvt.tag_id = t.id and usvt.owner_id = t.owner_id
group by usv_owner_id;
Here's the final solution in my case. Heavily based on the answer submitted by Gordon Linoff.
SELECT
user_submitted_value.id,
user_submitted_value.creator_id,
user_submitted_value.value,
group_concat(tag.text) AS tags
FROM user_submitted_value
LEFT JOIN user_submitted_value_tag
ON user_submitted_value.id = user_submitted_value_tag.value_id
AND user_submitted_value.creator_id = user_submitted_value_tag.creator_id
LEFT JOIN tag
ON user_submitted_valuetag.tag_id = tag.id
AND user_submitted_value_tag.creator_id = tag.creator_id
WHERE user_submitted_value.id = ?
GROUP BY user_submitted_value.id
The WHERE clause on the second JOIN can be modified to get all values for a given user.

How to insert data for one column for all the rows in single query in Mysql?

+----+------------+------------+------------+----------+
| id | phone_no | join_date | city | blood_gp |
+----+------------+------------+------------+----------+
| 1 | 80077672xx | 1997-07-19 | Delhi | NULL |
| 2 | 80077642xx | 1998-07-19 | New Delhi | NULL |
| 3 | 80477642xx | 1999-07-19 | Mumbai | NULL |
| 4 | 80077654xx | 1997-05-31 | Kolkata | NULL |
+----+------------+------------+------------+----------+
I want to enter all the blood groups at once . Is there a way to do so ?
you can use single query with select and update
UPDATE table1 , (SELECT * FROM table2 where 1) src
SET table1.blood_gp = src.filed2 where 1 ;
if you want to insert multiple row data using single query then use this code
INSERT INTO yourtable (x,y,z) VALUES (a1,a2,a3), (b1,b2,b3);
or if you want to update one column value all filed then use this code
update yourtable set blood_gp = 'yourvalue' where 1;
if any problem then inform me
Just make an update query without where clause.
update table set blood_gp = 'value'
That's generalize query.

merger one row with null values to not null values of another row mysql

I want to merge two rows into one.The below format is in the database.
+----+---------+-----------------------+-------------------------+
| id | appid | photo | signature |
+====+=========+=======================+=========================+
| 1 | 10001 | 10001.photograph.jpg | NULL |
| 2 | 10001 | NULL | 10001.signature.jpg |
+----+---------+-----------------------+-------------------------+
I want a mysql query so that i can fetch data like below,
+--------+------------------------+-------------------------+
| appid | photo | signature |
+========+========================+=========================+
|10001 | 10001.photograph.jpg | 10001.signature.jpg |
+--------+------------------------+-------------------------+
Kindly suggest...
You can also use max function
select appid,
max(photo) photo,
max(signature) signature
from test
group by appid
Demo
This should do this:
select t1.appid,t1.photo,t2.signature from mytable t1 join mytable t2 on t1.appid=t2.appid where t1.id=1 and t2.id=2

Mysql add column from different table

I have two tables:
| USER |
| _id | - int id PK
| id | - String id
| name |
| SUBSCRIBE |
| user |
| subscribe |
and I want to extract this data:
| _id | id | name | subscribe
Simple example
| USER |
| 1 | 10212551 | Mike
| 2 | 21022145 | Nick
| SUBSCRIBE |
| 1 | 2
get_info($user, $sub );
| EXPECTED RESULT |
| $user | id | name | 0 or 1 (false or true, if $sub has subscribed to $user)
My best try was with count():
SELECT u._id,u.id,u.name,u.email,u.country,count(s.subscribe) AS subscribe
FROM user u,subscribe s
WHERE u._id='$user' AND s.subscribe='$user' AND s.user='$sub'
But this works only if there's any record in subscribe table. So for
get_info(2,1)
it will give right result but for:
get_info(1,2)
there's nothing :/
Can somebody please help me with this ?
Use left join in order to return user even if he has not subscribed and use case to check for subscribed
select u._id,u.id,u.name,u.email,u.country,
case when s.subscribe is not null then 1 else 0 end as subscribe
from user u
left join subscribe s on (u._id = s.user and s.subscribe ='$sub')
where u._id='$user'
Demo