SQL structure with JOIN - php

I'm having some trouble figuring out how I should build my database for this project i'm currently working on. Fishing-related.
I'm just not sure how to set up my tables.
Table 1(ID, username, email etc)
Table 2(fish, weight, length etc)
How do i join these two tables? Should I have a column named ID in the 2nd table aswell? Because I need to know which user uploaded what fish. I'm just not sure how to do that.
Any help is appreciated.

Yes you have to, and that is called Relation Databases this is example
Users (UserID, UserName, Password)
Fish (FishID, UserID, FishName, Length, Weight)
and then you connect them using UserID
select u.UserName, f.FishName, f.Length, f.Weight
from Users u
LEFT JOIN Fish f on (f.UserID=u.UserID)
and if you are looking for specific user then just add at the end
WHERE u.UserID=#UserID

Looking at you're table structure I think it's best to change the id name in table 1 to *user_id* and add a column in the second table also named *user_id*. Joining using the columns is then very simple using the following query:
SELECT *
FROM table1
JOIN table2 USING (user_id)
Other possibility would be to add a column named *user_id* (or something else) to table2 and create a query like:
SELECT *
FROM table1
JOIN table2 ON table2.user_id = table1.id
In this case, you set the columns to use for the join in the 'ON .. = ..' structure.

Related

How to build table from two tables, one with useless unique identifier (WordPress wp_usermeta table)

Use case background: I'm using a WordPress site to manage memberships, and the ID Card software on my PC will connect to a table to generate its information. It can connect to only one table.
Goal: I need a table called "id_card" that contains the columns (member_number, first_name, last_name, exp_date), and it's populated with users that have been modified within the last month ("membership" table has a column titled "moddate" that I can use as the criteria).
Challenge 1: I need to pull exp_date from table "membership" and the other values -- member_number, first_name, last_name -- all come from a table called "wp_usermeta" (a default table for WordPress). Both source tables contain user_id to associate the information to the same user account. I believe this requires some kind of LEFT JOIN, but I haven't been able to get it to work.
Challenge 2: the bigger challenge: wp_usermeta table contains all the different user attributes in ROWS instead of COLUMNS. So the columns are titled "user_id, meta_key, meta_value" and there might be 30 rows for a single user. If I want to SELECT user 49's first name, it looks something like this:
SELECT meta_value FROM wp_usermeta WHERE user_id=49 AND meta_key='first_name'
Question: How do I write this query so it will INSERT INTO the table "id_card" the values from the two tables, using the user_id to keep it all lined up, especially when I can't use solely the user_id as a unique identifier (i.e. the only way I know to narrow it down to someone's first name is use both their user_id and the meta_key) in the wp_usermeta table?
There is a major problem with your wanting to copy data. If the user changes their data then your copy will be out of date. If you look at the wp_users table there is a ID field. This points to the relevant entries in the wp_usermeta table (user_id)
SELECT `wp_users`.*
, `wp_usermeta`.*
FROM `wp_users`
INNER JOIN `wp_usermeta` ON (`wp_users`.`ID` = `wp_usermeta`.`user_id`)
WHERE ID=1;
Now this will return multiple rows. If you want a single record then you'll need to do
SELECT`wp_users`.*
, (SELECT meta_key FROM wp_usermeta WHERE meta_key='wp_user_level' AND user_id=1) AS user_level
, (SELECT meta_key FROM wp_usermeta WHERE meta_key='show_syntax_highlighting' AND user_id=1) AS syntax_highlighting
FROM `wp_users`
WHERE ID=1;
Just repeat the subquery statements for as much info that you want to return. The subquery can only return a single value.

how to join two field from different table in sql

I have two different table in phpmyadmin. One is tbl_user and another is donate.
Now I want to take the column donation_date from tbl_user and all columns from the donate table. I want to join one column (donation_date) from tbl_user table and all cloumns from donate table, but dont know how to write the query.
In the below code I just wrote the query of donate table so how can I join the donation_date from the tbl_user.
Here is my details of two tables in phpmyadmin.
tbl_user table :
donate table:
$db = new PDO('mysql:host=localhost;dbname=mypro_bms','root','');
$statement = $db->prepare(
"insert into donate(passport_ic,blood_group,blood_bag,)
values(:passport_ic, :blood_group, :blood_bag)"
);
I want to take the column donation_date from tbl_user and all columns from the donate table.
Are you looking for... a simple JOIN between tables tbl_user and donate?
From your schema images, it looks like column passport_IPC can be used to join the tables. Then you can choose which columns to return in the SELECT clause:
SELECT u.donation_date, d.*
FROM tbl_user u
INNER JOIN donate d ON d.passport_IPC = u.passport_IPC
Looks like Passport_IC is the common column between the two tables?
You really should use be using numeric, indexed columns. But since you have a small DB, this should work:
SELECT d.*, u.donation_date
FROM donate d
INNER JOIN tbl_user u ON u.Passport_IC = d.Passport_IC
EDIT: You should give your a tbl_user table a primary key as well. Look into database normalization: https://en.wikipedia.org/wiki/Database_normalization
As you said, you want to take the column donation_date from tbl_user so you will select just the column nedded tbl_user.donation_date ans use the following alias donation_date and to get all columns from the donate table you can use this trick donate.*, it gets all column
SELECT
donate.* , tbl_user.donation_date as donation_date
FROM
tbl_user
JOIN
donate ON donate.passport_IPC = tbl_user.passport_IPC

How to change column name while joining the query in mysql

I need to change the column name while joining the MySQL query. I am explaining my code below.
select * from grc_action left join grc_users on grc_action.action_owner=grc_users.user_id
I am giving my table below.
grc_action:
id name action_owner
grc_users:
user_id name
The above is my table structure and as both has same column name i.e-name here I need to change the grc_users table column i.e-name while fetching the record. Please help me to solve this problem.
You can use AS
SELECT table1.name AS exampleName, table2.name AS otherName FROM sometable
You can also use this on tables:
SELECT vltn.id, vltn.name FROM veryLongTableName AS vltn
You dont need to type the AS, you can just do SELECT table1.name exampleName to shorten it, but it increases readbility and maintanability to write it, so I recommend doing it with AS.
You may assign different aliases to the name columns from the two different tables.
select
ga.id,
ga.name as action_name,
ga.action_owner,
gu.user_id,
gu.name as user_name
from grc_action ga
left join grc_users gu
on ga.action_owner = gu.user_id;
Note that I also used table aliases which make the query easier to read. In general, doing SELECT * is undesirable, and it is usually better to explicitly list the columns you want.
select *
from grc_action
left join grc_users on grc_action.action_owner=grc_users.user_id
changed query use this query
select *,grc_users.name as grcuser_name,grc_action.name as grcaction_name
from grc_action
left join grc_users on grc_action.action_owner=grc_users.user_id
Here geting two name like this
grcuser_name ,
grcaction_name

How to get data referenced in another table in one MySQL query

I would to select some data from mysql. However, some of the data stored in the table I am querying from are in codes and to get the text description I need to reference that data to another table.
TABLE: persons
SELECT id, first_name, last_name, address_code, customer_type_code
FROM persons
WHERE id = 1001
TABLE: ref_address
SELECT address_name FROM ref_address
WHERE address_code = 123
TABLE: ref_customer_type_code
SELECT customer_type_name FROM ref_customer_type_code
WHERE customer_type_code = 456
How can I combine all three queries together to return id, first_name, last_name, address_name, customer_type_name in one query instead of querying them 3 times like this?
Please read the reference manual for join.
In short, you need to define a relation between your tables (I use aliases just to make things a bit "cheaper" to write):
select p.id, p.first_name, p.last_name, p.address_code, p.customer_type_code
, ra.address_name
, rctc.customer_type_name
from persons as p
-- Join the persons table with the ref_address table,
-- using the address_code column of each table
inner join ref_adress as ra
on p.address_code = ra.address_code
-- Join the persons table with the ref_customer_type_code table
-- using the customer_type_code column of each table
inner join ref_customer_type_code as rctc
on p.customer_type_code = rctc.customer_type_code
where p.id = 1001
Notice that when you use multiple tables in a query it may be useful to define aliases to avoid having to write again and again the full name of the table. Also, it may be a good idea to explicitly specify each field's source table (by alias, if you are using it)
What you're looking for is a JOIN.
In a JOIN, you specify two tables and how they are related to one another. In a single SELECT statement, you can have multiple JOIN clauses.
SELECT
p.id, p.first_name, p.last_name, p.address_code, p.customer_type_code,
a.address_name,
t.customer_type_name
FROM
persons p
JOIN ref_address a
ON p.address_code = a.address_code
JOIN ref_customer_type_code t
ON p.customer_type_code = t.customer_type_code
WHERE
p.id = 1001
This query says that the table persons and ref_address should be linked, or "joined", by the related columns address_code which are available in each table. Same goes with the tables persons and ref_customer_type_code being linked by the columns customer_type_code.

SELECT command denied to user on database that doesn't exist

Been bothered with this for awhile now and i think it might be how i have the joins set up.
I have two tables. Ones is called info which contains all of a users contact information. My second table called numbers has all the phonenumbers for different users. They are related by the primary id of info to the info_id of phonenumbers. I want them to join based on this relationship and I want all the phonenumbers under phonenumbers to join into the single phonenumbers column in info. The current join i am using is this.
SELECT phonenumbers p, info i FROM i.phonenumbers
INNER JOIN p.workphone
ON i.PID=p.info_id
INNER JOIN p.homephone
ON i.PID=p.info_id
INNER JOIN p.mobilephone
ON i.PID=p.info_id
all i get is the SELECT comman is deneied to user on database workphone that isnt evena database.
table info:
PID,
firstname,
lastname,
address,
email,
phonenumbers,
table phonenumbers:
PID,
workphone,
homephone,
mobilephone,
info_id,
The syntax for a join would be nice. All the tutorials just give examples and not an explanation of what the different pieces are.
JOIN syntax is
TYPE_OF_JOIN database.table ON field = field
Since you have
JOIN p.workphone ON i.PID = p.info_id
You're actually telling the DB to look for a database named p, which contains a table workphone.
Doesn't matter that you've created an alias p up in your SELECT fields list. That's a field alias, and they NOT the same as a table alias.

Categories