473,371 Members | 1,479 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,371 software developers and data experts.

Query Join please help

Hi all,
I am a novice at SQL programming, I am in a fix with a new requirement…

I want a query that will fetch me username( from users table),
Ip_start and ip_end (from ip_range table) and accesstype (from subscription table) for corresponding user_no.

Like this
[user 1]
Username, 192.168.1.1. 192.168.1.1, read-only

[user 2]
example_user, 0.0.0.0, 0.0.0.9, read-only

Please help me!!!!

following are the table description.


users
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| users_no | int(11) | NO | PRI | NULL | auto_increment |
| username | varchar(255) | NO | | | |
| password | varchar(255) | NO | | | |
| email | varchar(255) | YES | | NULL | |
| firstname | varchar(255) | YES | | NULL | |
| middleinitial | varchar(10) | YES | | NULL | |
| lastname | varchar(255) | YES | | NULL | |
| companytype | varchar(255) | YES | | NULL | |
| companyname | varchar(255) | YES | | NULL | |
| department | varchar(255) | YES | | NULL | |
| role | varchar(255) | YES | | NULL | |
| title | varchar(10) | YES | | NULL | |
| mailinglist | varchar(255) | YES | | NULL | |
| question | varchar(255) | YES | | NULL | |
| answer | varchar(255) | YES | | NULL | |
| last_login | date | YES | | NULL | |
| last_login_ip | varchar(20) | YES | | NULL | |
| isadmin | varchar(255) | YES | | NULL | |
| block | varchar(255) | YES | | NULL | |
| creator | varchar(255) | YES | | NULL | |
| creationdate | date | YES | | NULL | |
| lastupdator | varchar(255) | YES | | NULL | |
| lastupdate | date | YES | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
(f.k - subscription2user. users_no )


ip_range
+-----------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| subscription_no | int(11) | NO | MUL | | |
| ip_start | varchar(20) | NO | | | |
| ip_end | varchar(20) | YES | | NULL | |
| hostname | varchar(100) | YES | | NULL | |
| creator | varchar(255) | YES | | NULL | |
| creationdate | date | YES | | NULL | |
+-----------------+--------------+------+-----+---------+-------+
foreign key (subscription_no) references subscription

subscription2user
+-----------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+---------+------+-----+---------+-------+
| subscription_no | int(11) | NO | MUL | | |
| users_no | int(11) | NO | MUL | | |
+-----------------+---------+------+-----+---------+-------+
foreign key (subscription_no) references subscription,
foreign key (users_no) references users

subscription
+------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+---------+----------------+
| subscription_no | int(11) | NO | PRI | NULL | auto_increment |
| customer_no | int(11) | NO | MUL | | |
| licensekey | varchar(30) | NO | | | |
| subscriptiontype | varchar(10) | NO | | | |
| begindate | date | NO | | | |
| enddate | date | NO | | | |
| maxusers | int(11) | YES | | NULL | |
| price | double | YES | | NULL | |
| regtype | varchar(10) | NO | | | |
| blockinvalidip | varchar(10) | YES | | NULL | |
| autorenewalmail | varchar(10) | YES | | NULL | |
| name | varchar(100) | NO | | | |
| password | varchar(20) | YES | | NULL | |
| creator | varchar(255) | YES | | NULL | |
| creationdate | date | YES | | NULL | |
| lastupdator | varchar(255) | YES | | NULL | |
| lastupdate | date | YES | | NULL | |
| accesstype | varchar(10) | YES | | NULL | |
+------------------+--------------+------+-----+---------+----------------+

Much Much Thanks!!!
Dec 18 '07 #1
3 1271
code green
1,726 Expert 1GB
want a query that will fetch me username( from users table),
Ip_start and ip_end (from ip_range table) and accesstype (from subscription table) for corresponding user_no
Try this
Expand|Select|Wrap|Line Numbers
  1. SELECT username,ip_start,ip_end, accesstype 
  2. FROM users
  3. JOIN subscription2user USING(user_no)
  4. LEFT JOIN ip_range ON subscription2user.subscription_no = ip_range.subscription_no
  5. LEFT JOIN subscription USING(subscription_no)
Can't see a need for subscription2user table
Dec 18 '07 #2
Try this
Expand|Select|Wrap|Line Numbers
  1. SELECT username,ip_start,ip_end, accesstype 
  2. FROM users
  3. JOIN subscription2user USING(user_no)
  4. LEFT JOIN ip_range ON subscription2user.subscription_no = ip_range.subscription_no
  5. LEFT JOIN subscription USING(subscription_no)
Can't see a need for subscription2user table


Tried got an error
<br>ERROR 1052 (23000): Column 'subscription_no' in from clause is ambiguous.
subscription2user table connects Subscription and user table <br>
Sorry no Foreign key at 'users' table.
Dec 19 '07 #3
code green
1,726 Expert 1GB
Column 'subscription_no' in from clause is ambiguous
This error means the engine cannot decide from which table 'subscription_no' is being referred.
Prefix it with a table name
Expand|Select|Wrap|Line Numbers
  1. `tablename`.`subscription_no` 
subscription2user table connects Subscription and user table
Yes I can see that. But why not use a common ID throughout all the tables.
Ah wait a minute. I think I understand.
There are multiple s`ubscription_no` to `user_no`.
Yes it is a good DB design
Dec 19 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Jim | last post by:
I need help on a query. There is a common titles database for several radio stations Some titles are enabled and some are not enabled for each station. Example of some tables: Titles ...
6
by: Mike | last post by:
I have had alot of problems with my query, I have a two tables of informations and just need a simple Query to get what I want but it is turning out not to be so simple. Table 1 Has the following...
4
by: d.p. | last post by:
Hi all, I'm using MS Access 2003. Bare with me on this description....here's the situation: Imagine insurance, and working out premiums for different insured properties. The rates for calculating...
4
by: bhargav.desai | last post by:
Hello Gurus, I need help! I have two table, tblCurrent and tblPrevious. What I want to join the tables, and create a new table that have matching records from both the tables, plus this new...
6
by: tizmagik | last post by:
I am having a lot of difficulty generating a CrossTab Query based report. I have looked online for several tutorials and whatnot but I have not been able to really find what I'm looking for, nor...
0
by: mlarson | last post by:
I have a program that worked fine then they needed to be able to also see the empty cells (inmate cells) on a housing unit when they ran the query. So what I had to do was take two tables and...
9
by: Frawls | last post by:
Hi I Am am having problems with a stored Procedure that i wrote. Basically whats happening is that the Stored procedure Runs fine when i EXECUTE it in SQL Query analyzer. But when i debug...
11
by: funky | last post by:
hello, I've got a big problem ad i'm not able to resolve it. We have a server running oracle 10g version 10.1.0. We usually use access as front end and connect database tables for data extraction....
3
by: Richard Hollenbeck | last post by:
I hope this isn't too confusing. The following query runs pretty fast by itself, but when I want to use it in a report (pasted below the query), it takes at least fifteen seconds to run! Then I...
1
by: write2ashokkumar | last post by:
hi... i have the table like this, Table Name : sample Total Records : 500000 (Consider like this) Sample Records: id ------------ name
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.