Mysql selet multiple values - php

As seen in the code below i have to select multiple values to be exported to my database. '_shipping_first_name' AND '_shipping_last_name'
How is it possible to make the MySQL work with two values connected?
SELECT meta_value FROM `wpd2_postmeta`
WHERE meta_key='_shipping_first_name'(value 2 here)
AND post_id='12017'

SELECT meta_value FROM wpd2_postmeta where meta_key=concat('_shipping_first_name', value2) and post_id='12017'

Related

How to select values from two tables in mysql prepared statements

For adding database I am using PhpMyAdmin in Xampp server. I have created two tables and each table contains different rows. I am trying to select values from two tables at the same time in php. The following code works fine
$gi1=2;
$stmt2=$this->con->prepare(" SELECT qwer FROM table1 WHERE nmnm=?;");
$stmt2->bind_param("i",$gi1);
$stmt2->execute();
$stmt2->bind_result($var1);
$stmt2->fetch();
$gi2=23;
$stmt2=$this->con->prepare(" SELECT qwer2 FROM table2 WHERE nmnm2=?;");
$stmt2->bind_param("i",$gi2);
$stmt2->execute();
$stmt2->bind_result($var2);
$stmt2->fetch();
But it takes too much time so I decided it to select in one statement.
$gi2=23;$gi1=2;
$stmt2=$this->con->prepare(" SELECT qwer2,qwer1 FROM table2,table1 WHERE nmnm2=? or nmnm=?;");
$stmt2->bind_param("ii",$gi2,$gi1);
$stmt2->execute();
$stmt2->bind_result($var2,$var1);
$stmt2->fetch();
And it shows a fatal error.So how can I select values from two tables in one statement?
if the tables do not have any link columns you can try with UNION
SELECT qwer FROM table1 WHERE nmnm=?
union
(SELECT qwer2 as qwer FROM table2 WHERE nmnm2=?)

copy one table to second table with current date

hy i am using Mysql (phpmyadmin) with php and i have two tables table1(id,name) and table2(id,date,name)
now i want to copy all rows of table1 to table2 and in table2 in date column should store current date of system, for this am trying following query which is giving error
'INSERT INTO table2 (id, Date, name)SELECT id,'.date("Y-m-d",mktime()).'nae FROM table1 WHERE id=2'
can you correct my query ? i tried different ways but not working any method. there is no error in connection with database all are ok, i tested connection its fine
How about this:
'INSERT INTO table2 (id, Date, name) SELECT id,"'.date("Y-m-d",mktime()).'",name FROM table1 WHERE id=2'

How to select sql query for two table with multiple values in php

I have two tables in my SQL Server database. The first is bidder_interest_name and second is tender_type_subcat . There have multiple value column bidder_interest_subcategory in bidder_interest_name and tender_type_subcategory in tender_type_subcat tables.
Now I want to select the multiple values from both the tables for a particular subcategory and need to both table minimum matching value.
This is what I'm doing
What I have tried:
SELECT bi.bidder_interest_subcategory,tt.tender_type_subcategory
FROM bidder_interest_list as bi,new_tender_two as tt
WHERE bi.bidder_id=$bidder_id
Try this
SELECT bi.bidder_id, bi.bidder_interest_subcategory,tt.tender_type_subcategory FROM bidder_interest_list as bi,new_tender_two as tt WHERE bi.bidder_id=$bidder_id;
Try this :SELECT bi.bidder_id=tt.tender_id, bi.bidder_interest_subcategory,tt.tender_type_subcategory FROM bidder_interest_list as bi,new_tender_two as tt WHERE bi.bidder_id=bidder_id;

MYSQL Insert with Select and if select returns more than 1 value insert both

Quite simple but I dont get it. Any Solution?
For example: if there is 2 times the name peter. But you should insert both.
insert into table1 (id,firstname,zip) values ('%s',(select nametable from table2 where name='peter'),'1234');
I hope its understandable
Edit: you get the id.. i just changed it...
I want to insert something where I have to select from another table. But the problem is sometimes i get more than 1 result. If there is more than 1 result I want to insert all results.
insert into table1 (id, name, zip) ...
And what would you like to insert into it?
... select id, nametable, '1234'
from table2
where name='peter'
Now you connect those 2 pieces and it should work

INSERT INTO different fields in different database table

I'm quite confused with sql INSERT INTO. Do I need to put JOIN? or INNER JOIN when I'm going to put into different table?
$sqlinsert = "INSERT INTO forum_topic (topic_title, topic_message, thread_id)
VALUES
('$topic','$message','$thread_id')";
I know this code is works for only one table but not for two. However, what I want is $message into post_message in other table.
In forum_topic table, there is post_id, post_message, date, and topic_id.
But there is forum_post table where I want to put $message into post_message in forum_post.
So, in forum_post table, there is post_id, post_message, date, and topic_id.
So do I need to JOIN or INNER JOIN? I don't know which one I should code them.
PS : I know the question is quite hard for you to understand. How I wish i can put image but I don't have 10 reputations, so i decided to do like this.
You cannot insert into multiple tables with one query, but you can use transactions to make sure that the operation is atomic.
BEGIN;
Your queries here
COMMIT;
When you need to execute several interdepending queries, some of them might result in error. So to keep the database consistent you would like to execute them as all-or-none (You can read about ACID).
MySQL also supports transactions but only with some of its storage engines (e.g. InnoDB, but not MyISAM).
The pseudo-code of usage looks like this.
start transaction
try {
execute query 1
execute query 2
...
commit transaction
catch {
rollback transaction
}
If you are using PDO, look at these functions.
In case of mysql_query, you can use
mysql_query("START TRANSACTION");
mysql_query("COMMIT");
mysql_query("ROLLBACK");
You don't use joins. You use separate queries for each table.
insert into table1
(field1, field2, etc)
values
(value1, value2, etc);
insert into table2
(field1, field2, etc)
values
(value1, value2, etc);
To insert into multiple tables you must use multiple queries.
Example:
$sqlinsert1 = "INSERT INTO forum_topic1 (topic_title1, topic_message1, thread_id1)
VALUES
('$topic1','$message1','$thread_id1')";
$sqlinsert2 = "INSERT INTO forum_topic2 (topic_title2, topic_message2, thread_id2)
VALUES
('$topic2','$message2','$thread_id2')";
Join are used for Selecting queries.
Example for a Join:
$sqlselect = "SELECT * FROM forum_topic
Join table2 on thread_id = table2id
WHERE topic_title = $topic";

Categories