select all from both tables with different aliases for id columns - php

Two tables: users and story
Want to select all from both tables, but need a different alias for users.id vs story.id
$sql = "select id as userid, name, status from users as t1, id as msgid, data, msg, xdir from story as t2 where...
Getting syntax error.
Any help?

You need to write a join. And you need to use table prefixes when referring to the column names that appear in both tables to distinguish them.
The entire SELECT list comes before the FROM and JOIN clauses.
SELECT u.id AS userid, name, status, s.id AS msgid, msg, xdir
FROM users AS u
JOIN story AS s ON s.authorid = u.id
WHERE ...

Related

Search in two tables and combine results with PDO

I have two tables named companies and jobs. They are related - jobs table has a foreign key company_id.
company table columns are: companyname, city, province etc.
jobs table columns: title, department, description etc
I want to search both tables in those columns and if the keyword has a match in jobs table get the related data from the company table, and if the match is in the company table, I want to get all the jobs related to the company.
As far as I know in order to search in both tables I have to use UNION, this works:
$sql= "(SELECT title, department FROM jobs WHERE title LIKE ? )
UNION
(SELECT companyname,city FROM companies WHERE companyname LIKE ?)";
I dont know how to get related data from the other table depending on the match. Is it possible with just one call using JOIN?
Thanks.
Use JOIN and OR condition:
SELECT jobs.title, jobs.department,
companies.companyname, companies.city
FROM jobs
JOIN companies ON companies.id = jobs.company_id
WHERE (jobs.title LIKE ? OR companies.companyname LIKE ?)
With UNION:
SELECT jobs.title, jobs.department,
companies.companyname, companies.city
FROM jobs
JOIN companies ON companies.id = jobs.company_id
WHERE jobs.title LIKE ?
UNION
SELECT jobs.title, jobs.department,
companies.companyname, companies.city
FROM jobs
JOIN companies ON companies.id = jobs.company_id
WHERE companies.companyname LIKE ?
Try this
SELECT a.title, a.department,c.companyname, c.city
FROM a
JOIN c ON c.id = a.company_id
WHERE (a.title LIKE 'title' OR c.companyname LIKE 'companyname')

MySQL INNER JOIN with different column names

Okay, so I have two tables, a news table and a users table. They are set up as below:
news:
id title user_id contents
users:
id username password
When I run this query:
SELECT news.title, users.username, news.contents FROM news INNER JOIN users ON news.user_id=users.id WHERE news.id=:id
I can only access the user id using $query['id']. So it appears that the join is using the column names from table2, although I want them to map it to the column names of table1. So even though the column is called id in the users table, I want to access it under user_id, since I specified that in the JOIN query.
I need this because if I ever need the id from table1, they would both be mapped to the column called id and I would only be able to get the id of the users table.
So is there any way to do this? Access the column from table2 under the name of the column in table1?
In MySQL what you specify in the select statement is what it is called in the result set, so you need to tell it what to give you. you do that with the AS command
SELECT users.id AS user_id ... FROM ...
That way you can call it whatever you want
or grab the user_id from news with
SELECT news.user_id ... FROM ...
SELECT users.id AS id1, news.id AS id2, news.title, users.username, news.contents
FROM news INNER JOIN users ON news.user_id=users.id WHERE news.id=:id
echo $query['id1'] . $query['id2'];

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.

my mysql result set is duplicating?why

im doing this query on mysql, but its giving me duplicate results sets:
this is my query:
SELECT u.username, u.picture, m.id, m.user_note, m.reply_id, m.reply_name, m.dt, m.votes_up
FROM user u, notes m
WHERE m.topic_id =14
ORDER BY m.dt DESC
i dont understand why its doing it? please help :))
EDIT: table schema
notes{id, user_id, topic_id, user_note, reply_id, reply_name, votes_up, dt}
user {user_id, username, password, email, full_name, picture, date_join}
You have no join condition on your where clause. Its create a Cartesian product of your results I would guess.
So you have two tables when you select from two tables it gives you the Cartesian Product of the results sets or all the rows in A X all the rows in B. What you need to do is link the two tables together ie to relate them. select * from User as U,Notes as N where U.ID = N.UserID Then add your other constraints.

Categories