I have two tables users and sellers where user_id and seller_id is different. I want to compare the search query on the first name and the last name of sellers and users and get the results. The two tables have no foreign key associates with it, and on the sellers table its just the seller name. No first name or last name.
SELECT *
FROM users
WHERE first_name LIKE "%'.$search_string.'%"
OR last_name LIKE "%'.$search_string.'%"
that works for the users table.. how to join it with the sellers table?
From the users table I would like to get user_id,first_name,last_name. From the sellers table I would like to get seller_id, seller_name.
I don't quite get what you're trying to accomplish, because you don't mention if there's a joinable field like, for example, transaction id, to relate users and sellers.
If you're searching just any users row or sellers row where the subject matches a search string, you might try with
SELECT 'users' as origin, CONCATENATE('first_name',' ','last_name') as name
FROM users
WHERE (first_name like '%".$search_string."%' OR last_name like '%".$search_string."%')
UNION
SELECT 'sellers' as origin, seller_name as name
FROM sellers
WHERE ( seller_name like '%".$search_string."%')
keep in mind that, if you remove the 'origin' column, the UNION statement will perform an implicit DISTINCT on the resultset. If you need to display dupe results then you should use UNION ALL.
Related
I'm trying to write a SQL statement that outputs a table with the two columns show name and number of seasons. The show column must contain no duplocates and the number of seasons column counts the number of seasons associated with the show entity.
Here are my two tables
Shows Table
id | name
Seasons Table
id | show_id | season_number
Here's what I've tried so far
SELECT DISTINCT shows.name
FROM shows
INNER JOIN seasons on show.id = seasons.show_id;
The above code works for grabbing distinct names but whenever I try adding COUNT(season.id) it breaks.
Any suggestions?
Thanks!
Use group by to aggregate multiple rows with the same name into a group. With count(distinct id) you calculate the number of distinct values of the id column in that group.
SELECT name
, COUNT(DISTINCT seasons.id)
FROM shows
JOIN seasons
ON shows.show_id = seasons.show_id
GROUP BY
name
By the way, I'd expect a season to have a one to many relation to show, not the other way around.
My table structure is:
Customers(customerid,first name,last name,state)
I want to print the name of the customers who belong to the same state and if they are from states where no other customer lives then that customer should be omitted..I tried inner joins but couldn't get the exact results I get one or more extra rows.
The following is from Access SQL, which should work fine for you.
Select customer_id, state, last_name, first_name
FROM Customers
WHERE (((state) In (Select state FROM Customers GROUP BY state HAVING (((Count(state))>1)))))
ORDER BY state,last_name, first_name
I am trying to create a view with the following results. I can create the first part just fine but the second part is where I am having trouble. When I do the join I am receiving an Error Code: 1052. Column 'customer_id' in field list is ambiguous. Now I know that the in the two tables I have duplicate columns.
My question is how do I write the view to ignore one of the duplicate columns
Create a view named customer_addresses that shows the shipping and billing addresses for each customer. This view should return these columns from the Customers table:
customer_id
email_address
last_name
first_name.
This view should return these columns from the Addresses table:
bill_line1
bill_line2
bill_city
bill_state
bill_zip
ship_line1
ship_line2
ship_city
ship_state
ship_zip
The rows in this view should be sorted by the last_name and then first_name columns.
Here is my code.
CREATE OR REPLACE VIEW customer_addresses
AS
SELECT customer_id, email_address, last_name, first_name, addresses.line1
FROM customers JOIN addresses
ORDER BY last_name
I would just identify all the columns like this:
CREATE OR REPLACE VIEW customer_addresses
AS
SELECT
customers.customer_id,
customers.email_address,
customers.last_name,
customers.first_name,
addresses.line1
FROM customers JOIN addresses
ORDER BY customers.last_name
Because I believe that you have an customer_id in the customers table and one in the addresses table. Right?
If you do not specify the table name and add a column to one of the tables with the same column name as in the view the problem will arise again.
I have a website with products where some products are duplicated, and the duplication is only because sometimes the same products goes under more than one categories. I just want the unique columns of the product, not the duplicate (that has another ID and another Category_id). I know the problem could be solved if the table was normalized, but I didn't develop these tables and I can't redesign the database now.
So basically I'm trying to something that logically looks like this (but the code below still gets the repeated products):
SELECT id
FROM `website_products`
WHERE p_name_en
IN (
SELECT DISTINCT p_name_en
FROM `website_products`
)
Do you just want:
select distinct id
from website_products
Or are you trying to get distinct product names with a single id:
select p_name_en, id
from website_products wp
group by p_name_en;
You Can Try like this,,,
SELECT id
FROM `website_products
group by p_name_en
MySQl Comparison of Two Different Tables
I have two tables with these structures:
Table 1
FullContact
-----------
id
name
phone
address
email
phone
registration
marketingId
created
Table 2
Contacted
-----------
first_name
last_name
email
The FullContact table contains people and the Contacted might contain the same individuals but is given back to me in a strange column structure.
The FullContact might contain duplicates and the Contacted has no duplicates.
Main Goal: I want to see how many from Contacted are in FullContact.
The query I am trying to run is of this form:
SELECT distinct fc.name, fc.email, fc.phone, concat_ws(" ",c.first_name, c.last_name),
c.phone from Contacted c
INNER JOIN FullContact fc
ON ( concat_ws(" ", c.first_name,
c.last_name) = fc.name or c.email = fc.email or (fc.phone REGEXP '[0-9]+$') = c.phone)
and fc.created >= '2011-11-01';
Explanation of Query:
I am trying to select the items in FullContact that are the same as Contacted by using an inner join and constraining by three string parameters and a date parameter.
Problem:
I made the select statement to show the results of items from FullContact and items from Contacted. The results contain FullContact columns being unique and Contact values duplicating.
Is this the correct query to run?
Thanks
You are checking for three lots of duplicates in there, name, email or phone.
So if you have two different names with the same email, you get two records in the result. I would have expected and in the on clause.