SQL - copy data from one table column to another table column - php

As you can see from the image I have a table called wpw8_postmeta where it has four columns and I have searched for the term release and got the following results.
I want to copy the results (release_date [meta_value]) to another table called wpw8_test using the post_id as foreign key/primary key.
The second table looks like this
After the update will be done it should look like this

You seem to be looking for the update ... join syntax:
update wpw8_test t
inner join wpw8_postmeta p on p.post_id = t.post_id
set t.release_date = p.meta_value
where p.meta_key like '%release%'

Related

Get CSV from query which involves multiple tables

I'm trying to make a query to extract elements from 2 tables, which are linked via another table.
So I have 3 tables:
authors
- id, name, book
category
- id, name, description
category-author
- id, idauthor, idcategory
Now I want to make a query to make the following output:
row: authors.id, authors.name, authors.book, category.name
I don't know what category's are linked using the 2 tables only, so I need to use the last one, the category-author table. The id's of both the author and the category are linked via that table.
I got the following query:
SELECT authors.id, authors.name, authors.book, category.name FROM category, author LEFT JOIN SELECT ??
I'm stuck at the remaining part of the query.
Also when I have this query, can I just extract a CSV with phpmyadmin?
You can get related information from different tables using table joins. Relations between tables should be specified using foreign keys (i.e. the column idcategory from category-author is presumably a foreign key that refers to primary key column category.id). In a join clause, you merely specify which tables are to be joined and on what column:
SELECT table1.col1, table2.col2
FROM table1
JOIN table2 ON table1.pkCol = table2.fkCol
This means you can't specify any SELECT or FROM clauses within a JOIN clause. The columns you wish to select from joined tables are all specified in the initial SELECT statement, and you only specify one table in the FROM clause, from which you subsequently perform the table joins.
In your case, I think this should get you started:
SELECT authors.id, authors.name, authors.book, category.name
FROM category
LEFT JOIN category-author ON category-author.idcategory = category.id
LEFT JOIN authors ON authors.id = category-author.idauthor
I'm not sure how familiar you are with foreign keys, primary keys and table joins, so I won't elaborate any more on this. I think specifying multiple tables in a FROM clause is bad practice, even if your database system still supports it (Related question).
From then on, you can easily export the results from within PhpMyAdmin, as there is an export button for every table overview, including query results.

How to get value from one table column when two columns of the same name exist in an sql join

I am currently faced with the problem of trying to get a value when I have joined two tables with columns of the same name eg: table1.date and table2.date (date is different in each table) how would I get the "date" of table 1 in this instance.
I am currently running
while($row = $mysqliquery->fetch_object()) {
is there any way that I could use some form of syntax to retrieve the date from table 1 within the following code
$row->date eg $row->table1.date or something
If not how else would I be able to accomplish this?
Thanks.
You should differentiate between 2 columns with the same name by using an alias for one or both of the 2 columns in the query like this
SELECT a.`date`, b.`date` as b_date
FROM table1 a
JOIN table2 b ON a.id = b.a_id
WHERE some specific criteria
Now when your retrieve the ROW each date has its own unique name i.e.
$row->date;
$row->b_date;
I was thinking more along the lines of this ( ficticious ) pseudo sql
select a.`account_id`, a.`date` as 'account_date', u.`date` as 'signup_date'
from `accounts` a
left outer join `users` u on u.`uid`=a.`uid`
Each table has a column called date but in the sql they are each referenced with a unique alias. You cannot have two columns in the same query with the same name or you'll get errors, so you give them an alias. The tables are also aliased in this code - it really helps simplify things in my opinion... Anyway, just a quick example to illustrate how you might approach this - others may have a different approach from which I might learn new tricks

MySQL - Query a temp table to retrieve 2 rows from table

Thank you for taking the time to read my question.
I am trying to combine data from 2 tables in such a way that it can be presented on a webpage with PHP -- I am using a WordPress site so the table structure may look familiar.
The table layout:
wp_3bpf9a_posts - This table has 1 row for each 'post', and I need this table so I can query for a specific type of post, and also obtain the date.
wp_3bpf9a_postmeta - Table with many records for each post (there is an ID that links the two together). I actually need data from 2 (of many) rows for each ID to complete the displayed dataset I am looking for.
I tried to accomplish this by creating a temp table joining the first record together between both tables, and then I was going to go back and join the temp table to the second record I needed. I actually connected to MySQL DB using Toad, and got the code below to work when I run each statement below one at a time in sequence. When I try to run the whole thing I do not get an error but also get no results, same when I try it on my PHP page. I know there is likely an easier way to do this, but this was my first step out of a simple MySQL query. Any guidance that you could provide would be appreciated, thanks!
DROP TABLE IF EXISTS bookingTempTable;
CREATE TEMPORARY TABLE bookingTempTable AS
SELECT ID, post_title, post_name, meta_value
FROM `wp_3bpf9a_posts`
INNER JOIN `wp_3bpf9a_postmeta` ON `wp_3bpf9a_postmeta`.`post_id`=`wp_3bpf9a_posts`.`ID`
WHERE `post_type` LIKE 'booked_appointments' AND `post_title` LIKE '%October 9%' AND `wp_3bpf9a_postmeta`.`meta_key` LIKE 'slot_members';
SELECT *
FROM `wp_3bpf9a_postmeta`
INNER JOIN `bookingTempTable` ON `bookingTempTable`.`ID`=`wp_3bpf9a_postmeta`.`post_id`
WHERE `wp_3bpf9a_postmeta`.`meta_key` LIKE '_appointment_timeslot'
ORDER by `wp_3bpf9a_postmeta`.`meta_value` ASC;
You don't need the temporary table, just join with wp_sbpf9a_postmeta twice.
SELECT p.*, m1.meta_value AS slot_member, m2.meta_value AS appointment_timeslot
FROM wp_sbpf9a_posts AS p
JOIN wp_3bpf9a_postmeta AS m1 ON p.id = m1.post_id
JOIN wp_sbpf9a_postmeta AS m2 ON p.id = m2.post_id
WHERE p.post_type = 'booked_appointments'
AND p.post_title LIKE '%October 9%'
AND m1.meta_key = 'slot_members'
AND m2.meta_key = '_appointment_timeslot'
ORDER BY appointment_timeslot

Joining two tables together with two foreign keys

I have a Description table which contains certain descriptions along with a unique ID. I have another table that contains two foreign keys to this table. So far i have the following query:
SELECT
Description.description AS Description,
Object.objID AS ID,
Description.description AS Location
FROM
Object
INNER JOIN
Description
ON
Object.objDescID=Description.descID
AND
Object.objLocID=Description.descID;
However this is not working, please can someone point me in the right direction?
If I understand right you want to join to the Description table twice for the same object. Give this a shot and see if it gets you what you're after:
SELECT
Object.objID AS ID,
od.description AS Description,
ld.description AS Location
FROM Object
INNER JOIN Description AS od
ON Object.objDescID=od.descID
INNER JOIN Description AS ld
ON Object.objLocID=ld.descID;
Edit: A word of advice, if you allow for null foreign keys you should use a LEFT JOIN instead of an INNER JOIN, that way if one of them is null it doesn't keep the entire record from showing.
Try Running This (might need minor adjustments):
SELECT
Description.description AS Description,
Object.objID AS ID,
Description.description AS Location
FROM
Object
INNER JOIN
Description AS Object.objDescID=Description.descID
INNER JOIN
Description AS Object.objLocID=Description.descID;
Looks like you need two references to the Description table. Each reference will be joined using one of the foreign key columns.
For example:
SELECT o.objID AS `ID`
, d.description AS `Description`
, l.description AS `Location`
FROM Object o
JOIN Description d
ON d.descID = o.objDescID
JOIN Description l
ON l.descID = o.objLocID
We assign the short alias d to the source we get the Description value from.
We assign the short alias l to the source we get the Location value from.
We reference columns from each table using the short alias, rather than the table name.
Essentially, think of the references to the Description table like it's two different tables, even though it's really the same table.
Note that we have to assign an alias to at least one of the references to Description, so that we can distinguish between them. (Otherwise, MySQL won't know which one we're talking about if we just said Description.description.)
Note that if the foreign key column objDescID or objLocID has a NULL value, or a matching value doesn't exist in the referenced table, the query won't return the row from Object.
To ensure you get a row from Object even when the matching values aren't found, you can use an OUTER join operation by including the LEFT keyword.
For example:
SELECT o.objID AS `ID`
, d.description AS `Description`
, l.description AS `Location`
FROM Object o
LEFT
JOIN Description d
ON d.descID = o.objDescID
LEFT
JOIN Description l
ON l.descID = o.objLocID
Note that only one alias is actually required, but I tend to assign short aliases to all row sources in a query. This makes the statement more decipherable, and really helps if I later need to add another reference to a table that is already used, or if I need to replace one of the table names with a different table name or an inline view (or subquery), I can leave the alias the same, and change just the rowsource. The other aliases don't make any difference in the actual execution of the statement, they are just there because I follow the same pattern for simple queries that I follow for more complex queries.

Getting SQL result as nested array in PHP

I've a ('courses') table that has a HABTM relationship with ('instructors') table through another table...
I want to get the data of an instructor with all related courses in one query..
Currently, I have the following SQL:
SELECT *
FROM `instructors` AS `instructor`
LEFT JOIN `courses` AS `course`
ON `course`.`id` IN (
SELECT `course_id`
FROM `course_instructors`
WHERE `course_instructors`.`instructor_id` = `instructor`.`id`
)
WHERE `instructor`.`id` = 1
This SQL does what it should be doing, the only "problem" I have is that I get multiple rows for each joined rows.
My question is:
Can I get the result I want in one query? Or do I have to manipulate the data in PHP?
I'm using PHP and MySQL.
Each record of a query result set has the same format: same number of fields, same fields, same order of fields. You cannot change that.
SELECT *
FROM instructors AS instructor
LEFT JOIN
course_instructors
ON
instructor.id= course_instructors.instructor_id
LEFT JOIN
courses
ON
course_instructors.course_id = course.id
WHERE instructor.id = 1
This assumes the PK of course_instructors is (instructor_id,course_id)
Explanation of query:
First join + WHERE make sure you get the relevant instructor
Second join matches ALL the entries from the course_instructor table that belongs to this instructor. If none found, will return one row with NULL in all fields
Last join matches all relevant courses from the entries found from course_instructor If none would will return one record with NULL in all fields.
Again: important to use the right constraints to avoid duplicate data.
That's the nature of relational databases. You need to get the instructor first and then get the related courses. That's how I would do it and that's how I've been doing it. I'm not sure if there is a "hack" to it.

Categories